I have some XML:
<messages>
<message>
<meta appid="1112" date="20111028" msgid="4498016" msgno="855" permlinkdate="2011-10-28">
<summary>RedBook is a fun book</summary>
</meta>
<ISBN>1234123412123</ISBN>
</message>
<message>
<meta appid="1112" date="20111028" msgid="4498016" msgno="855" permlinkdate="2007-1-30">
<summary>BlueBook is a good book</summary>
</meta>
<ISBN>123412341234</ISBN>
</message>
</messages>
And I have some Linq to XML to operate on this XML:
public static List<ReviewDTC> GetReviews()
{
XElement xmlDoc = XElement.Load(@"C:\Users\inelson\Desktop\fiddle.xml");
var dtos = from item in xmlDoc.Descendants("message")
select new ReviewDTC()
{
PubDate = item.Element("meta").Attribute("permlinkdate").Value,
Summary = item.Element("summary").Value,
Isbn = item.Element("ISBN").Value
};
List<ReviewDTC> reviews = new List<ReviewDTC>();
reviews = dtos.ToList();
return reviews;
}
The purpose of the above code is to parse the document for each message node, and for each message node, create an object with that node’s child elements (and one attribute). Each object is added to a collection and so on..
When debugging this code, I place a BP after var dtos = and it is asserting that dtos is not being set to an instance of an object when it is instantiating a new ReviewDTC object. Is the parser not able to read from Meta, Summary or ISBN to build the object? Whats going on?
I appreciate it, big time.
The
summaryelement is under themetaelement.Try this: