鉴于此xml文档
<listOfItem>
<Item id="1">
<attribute1 type="foo"/>
<attribute2 type="bar"/>
<property type="x"/>
<property type="y"/>
<attribute3 type="z"/>
</Item>
<Item>
//... same child nodes
</Item>
//.... other Items
</listOfItems>
给定这个xml文档,我想为每个“项目”节点选择“属性”子节点。我如何直接在C#中做到这一点?“直接”是指不选择Item的所有子节点,然后逐个检查。至今:
XmlNodeList nodes = xmldoc.GetElementsByTagName("Item");
foreach(XmlNode node in nodes)
{
doSomething()
foreach(XmlNode child in node.ChildNodes)
{
if(child.Name == "property")
{
doSomethingElse()
}
}
}
您可以使用SelectNodes(xpath)
method而不是ChildNodes
property:
foreach(XmlNode child in node.SelectNodes("property"))
{
doSomethingElse()
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句