鉴于此 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 文档,我想为每个“Item”节点选择“property”子节点。我怎样才能直接在c#中做到这一点?“直接”是指不选择 Item 的所有子节点,然后一一检查。至今:
XmlNodeList nodes = xmldoc.GetElementsByTagName("Item");
foreach(XmlNode node in nodes)
{
doSomething()
foreach(XmlNode child in node.ChildNodes)
{
if(child.Name == "property")
{
doSomethingElse()
}
}
}