【发布时间】:2012-05-14 15:29:38
【问题描述】:
假设我有一个这样的 XmlDocument:
<xmlFile>
<details>
<code1>ADJ</code1>
<code2>ADC </code2>
<Shipment>
<foo></foo>
<bar></bar>
</Shipment>
<Shipment>
<foo></foo>
<bar></bar>
</Shipment>
</details>
<details>
<code1>ADJ</code1>
<code2>SCC </code2>
<Shipment>
<foo></foo>
<bar></bar>
</Shipment>
</details>
</xmlFile>
我需要在一个 xml 文件中处理每个,但只处理属于具有值为“ADC”的子节点的标签下的货件。到目前为止,我有:
// Assume there is an XmlDocument named xml
XmlNodeList details= xml.GetElementsByTagName("details");
foreach (XmlNode node in details)
{
if (node["code2"].InnerText == "ADC ")
{
// Get each shipment and process it accordingly.
}
}
我不知道下一步该做什么。谢谢。
【问题讨论】:
-
“Under”=“在文本之下”还是“一个孩子”? (
<codeX>没有一个有意义的孩子。) -
你能用 XDocument 和 LINQ 代替吗?
-
您将如何使用 LINQ 和 XDocument 实现它?现有代码库使用所有 XmlDocument。
-
第一个细节的代码2是“ADC”而不是“ADC”,所以它不会匹配。
标签: c# .net xmldocument