I am using this code to retrieve the values I want from XML:
IEnumerable<ForewordReview> reviews = null;
try
{
reviews = from item in xmlDoc.Descendants("node")
select new ForewordReview()
{
PubDate = item.Element("created").ToString(),
Isbn = item.Element("isbn").ToString(),
Summary = item.Element("review").ToString()
};
} // ...
Incidentally, a client is now passing us almost every tag with CDATA which I need to extract:
<review>
<node>
<created>
<![CDATA[2012-01-23 12:40:57]]>
</created>
<isbn>
<![CDATA[123456789]]>
</isbn>
<summary>
<![CDATA[Teh Kittehs like to play in teh mud]]>
</summary>
</node>
</review>
I have seen a couple of solutions for extracting these values from within the CDATA tag, one of which is to use a where clause on the LINQ statement:
where element.NodeType == System.Xml.XmlNodeType.CDATA
I sort of see whats going on here, but I am not sure this works with how I am using Linq (specifically, building an object from selected items.
Do I need to apply this filter on the items in the select statement individually? Otherwise, I dont really understand how this will work with the code I am using.
As always, I appreciate the help.
Cast each
XElementto astringinstead:This also works with other data types, such as
int,float,DateTime, etc:It also works with
XAttributes as well.