Sunday, May 15, 2011

A simple example to say the difference between Descendants vs Elements

Here is the XML









if use Descendants

XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");

var result = from book in xDoc.Descendants("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};

if use Elements

XDocument xDoc = XDocument.Load(@"C:\Users\anishmarokey\Desktop\XMLFile1.xml");

var result = from book in xDoc.Elements("Titles").Elements("Book")
select new
{
Title = book.Attribute("Title"),
Author = book.Attribute("Author")
};

No comments:

Post a Comment