Xml SelectNodes不受XmlNodeList限制

时间:2019-05-13 05:54:37

标签: xml xmlnode xmlnodelist

我正在尝试通过SelectNodes将XmlNodeList拆分为XmlNodes。我可以打印出拆分后的XmlNodeList的内容,但是当我在单独的XmlNode testNode上运行foreach时,它在原始NodeList上运行,而不是在数组中的单个XmlNode上运行。

XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(new StringReader(xmlData));
    string xmlPathPattern = "//data/set";
    XmlNodeList setNodeList = xmlDoc.SelectNodes(xmlPathPattern);
    XmlNode firstSetNode = setNodeList[0];
    string xmlPathPattern2 = "//word";
    XmlNodeList setListOne = firstSetNode.SelectNodes(xmlPathPattern2);
    foreach (XmlNode wordnode in setListOne)
    {
        XmlNode english = wordnode.FirstChild;
        XmlNode infinitive = english.NextSibling;
        XmlNode audio = infinitive.NextSibling;
        string englishString = english.InnerXml;
        string infinitiveString = infinitive.InnerXml;
        string audioString = audio.InnerXml;

    }

我从firstSetNode.InnerXml.ToString()或.innerTest.ToString()获得的打印结果仅包含第一组,但是setListOne上的foreach循环遍历整个原始setNodeList,即我的foreach字符串包含所有话。我在这里缺少什么吗?谢谢

我的xml的结构为

<data>

<set>
    <word>
        <english>I open</english>
        <spanish>abro</spanish>
        <audio>abro</audio>
    </word>
    <word>
        <english>you (tú) open</english>
        <spanish>abres</spanish>
        <audio>abres</audio>
    </word>
    <word>
        <english>you (Ud) open</english>
        <spanish>abre</spanish>
        <audio>abre</audio>
    </word>
    <word>
        <english>he/she opens</english>
        <spanish>abre</spanish>
        <audio>abre</audio>
    </word>
    <word>
        <english>we open</english>
        <spanish>abrimos</spanish>
        <audio>abrimos</audio>
    </word>
    <word>
        <english>you all/they open</english>
        <spanish>abren</spanish>
        <audio>abren</audio>
    </word>
</set>
<set>
    <word>
        <english>I finish</english>
        <spanish>acabo</spanish>
        <audio>acabo</audio>
    </word>
    <word>
        <english>you (tú) finish</english>
        <spanish>acabas</spanish>
        <audio>acabas</audio>
    </word>
    <word>
        <english>you (Ud) finish</english>
        <spanish>acaba</spanish>
        <audio>acaba</audio>
    </word>
    <word>
        <english>he/she finishes</english>
        <spanish>acaba</spanish>
        <audio>acaba</audio>
    </word>
    <word>
        <english>we finish</english>
        <spanish>acabamos</spanish>
        <audio>acabamos</audio>
    </word>
    <word>
        <english>you all/they finish</english>
        <spanish>acaban</spanish>
        <audio>acaban</audio>
    </word>
</set>

0 个答案:

没有答案