说我有一个这样的XML文档
<xml>
<food>
<banana>this is a banana</banana>
<apple>this is an apple</apple>
<grape>this is a grape</grape>
</food>
<food>
<cake>this is cake</cake>
<soda>this is soda</soda>
<cookie>this is a cookie</cookie>
</food>
</xml>
[我将如何跳到<food>
的第二个节点以使用C#中的XMLDocument从那里获取食物?任何建议表示赞赏!
XmlDocument xdcDocument = new XmlDocument();
xdcDocument.LoadXml(<xml string>);
XmlElement xelRoot = xdcDocument.DocumentElement;
XmlNodeList xnlNodes = xelRoot.SelectNodes("/food");
bool first = true;
foreach(XmlNode xndNode in xnlNodes)
{
if (first) {
first = false;
continue;
}
// process the second node here
}
您可以使用Enumerable.Skip
(来自@JeroenvanLangen的想法,但需要一点技巧: