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);
}
// XML document
<Config>
<Profile>
<id>Scope</id>
<username>Scope 1</username>
<password>...</password>
<cdkey>0000</cdkey>
<expkey></expkey>
<cdkeyowner>Scope</cdkeyowner>
<client>W2BN</client>
<server>[IP]</server>
<homechannel>Lobby</homechannel>
<load>1</load>
<order>2</order>
</Profile>
<Profile>
<id>Scope 2</id>
<username>Scope 2</username>
<password>...</password>
<cdkey>0000</cdkey>
<expkey></expkey>
<cdkeyowner>Scope</cdkeyowner>
<client>W2BN</client>
<server>[IP]</server>
<homechannel>Lobby</homechannel>
<load>1</load>
<order>1</order>
</Profile>
</Config>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…