如何在C#中跳过XMLDocument中的节点

问题描述 投票:0回答:2

说我有一个这样的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从那里获取食物?任何建议表示赞赏!

c# xml xmldocument
2个回答
1
投票
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
}

0
投票

您可以使用Enumerable.Skip(来自@JeroenvanLangen的想法,但需要一点技巧:

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.