【发布时间】:2011-02-14 07:16:12
【问题描述】:
我正在尝试遍历我的 xml 文档的节点以获取每个节点中 <username>Ed</username> 的值。我首先使用 Linq 对 XDocument 进行排序,然后尝试遍历节点。我似乎找不到正确的 foreach 循环来实现这一点。任何帮助表示赞赏。
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>
【问题讨论】:
-
您能发布您的(删节的)架构或(删节的)示例文件吗?
-
当然,
codeEd Ed 2 code -
您应该能够在此处放置断点,在调试模式下运行,然后检查
newDoc对象以确定要查询的正确 XML 属性。 -
将架构添加到您的问题而不是评论中,它会更容易参考
-
抱歉,我已将其添加到问题中。