How to iterate through an XDocument's Nodes

Posted by Ed R on Stack Overflow See other posts from Stack Overflow or by Ed R
Published on 2011-02-14T07:16:12Z Indexed on 2011/02/14 7:25 UTC
Read the original article Hit count: 157

Filed under:
|
|
|
|

I am trying to iterate through my xml document's nodes to get the value for <username>Ed</username> in each node. I am using Linq to sort the XDocument first, then attempting to loop through the nodes. I can't seem to find the correct foreach loop to achieve this. Any help is appreciated.

var doc = XDocument.Load("files\\config.xml");
var newDoc = new XDocument(new XElement("Config",
            from p in doc.Element("Config").Elements("Profile")
            orderby int.Parse(p.Element("order").Value)
            select p));


foreach (XElement xe in newDoc.Nodes())
{
    MessageBox.Show(xe.Element("username").Value);
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET