How to iterate through an XDocument's Nodes
- by Ed R
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);
}