Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#

Reading XML documents using LINQ

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
14 Jan 2011CPOL 9.8K   1  
Not really that big of a change, but I'd go with an XPath solution. It locates all of the "magic strings" into one spot.var nodes = from element in XElement.Load("Books.xml").XPathSelectElements("/Books/Book/Subject") select new { Value =...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
15 Jan 2011Anupama Roy 2 alternatives  
Reading XML documents using LINQ
Please Sign up or sign in to vote.
18 Jan 2011Richard Deeming
Another option would be to use the Elements extension method[^]:var root = XElement.Load("Books.xml"); var books = from node in root.Elements("Book").Elements("Subject") select node.Value.Trim(); foreach (string subject in books){ listBox1.Items.Add(subject);}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions