XMLNodeList没有检索正确的节点

时间:2014-11-16 22:52:32

标签: c# xml xaml

我正在创建应用程序来制作现代战争3(XRPC)的Mod Menu。我正在使用XML进行自定义,我创建了一个Example XML。

<MenuFile>
<Credits>
    <Author>Callum Carmicheal</Author>
    <Name>Sample</Name>
    <Version>0.1</Version>
</Credits>

<InfoBar>
    <Text>Created by ..., Enjoy</Text>
</InfoBar>

<Menu>

    <Main>
        <Header> 
            <Name>Main Menu</Name>
        </Header>

        <Item>
            <Name>Enable Cheats</Name>
            <Function>
                <Menu>Mods</Menu>
            </Function>
            <Ignore>false</Ignore>
        </Item>

        <Item>
            <Name> </Name>
            <Function> </Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>
    </Main>

    <Mods>
        <Header>
            <Name>Mods</Name>
        </Header>

        <Item>
            <Name>Enable Cheats</Name>
            <Function>
                <Dvar>sv_cheats 1</Dvar>
            </Function>
            <Ignore>false</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>

        <Item>
            <Name></Name>
            <Function></Function>
            <Ignore>True</Ignore>
        </Item>
    </Mods>

</Menu>

</MenuFile>

这是菜单的标记,我创建了一些c#Code

        private void button1_Click(object sender, EventArgs e) {
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(System.IO.File.ReadAllText("menu.xml")); // suppose that myXmlString contains "<Names>...</Names>"

        XmlNode credits_author = xml.SelectSingleNode("/MenuFile/Credits/Author");
        XmlNode credits_name = xml.SelectSingleNode("/MenuFile/Credits/Name");
        XmlNode credits_version = xml.SelectSingleNode("/MenuFile/Credits/Version");
        Log("Credits(Author): " + credits_author.InnerText);
        Log("Credits(Name): " + credits_name.InnerText);
        Log("Credits(Version): " + credits_version.InnerText);

        XmlNode InfoBar = xml.SelectSingleNode("/MenuFile/InfoBar/Text");
        Log("");
        Log("InfoBar Text: " + InfoBar.InnerText);

        Log("");
        Log("Menus");

        XmlNodeList Menus = xml.SelectNodes("/MenuFile/Menu");
        // This returns 
        //   /MenuFile/Menu not /MenuFile/Menu/MainMenu....
        // it should loop through MainMenu, Mods...
        foreach (XmlNode MenuItem in Menus) {
            Log("\t Menu: " + MenuItem.Name); // Main should me MainMenu or Mods

            XmlNodeList Items = xml.SelectNodes("/MenuFile/Menu/" + MenuItem.Name);
            foreach (XmlNode mi in Items) {
                if (mi.Name == "Header") {
                    string xpath = FindXPath(mi);
                    string Header = xml.SelectSingleNode("/MenuFile/Menu/" + MenuItem.Name + "/Header/Name").InnerText;
                    Log("\t\t Header: " + Header);
                }
                else if (mi.Name == "Item") {
                    string Name = "";
                    string ignore = "";
                }
            }

        }

    }

但问题是在尝试检索MenuList / Menu的子节点时,它返回MenuList / Menu本身,我不知道。 我已经尝试过调试但结果是一样的,有人能看到我的错误吗?

1 个答案:

答案 0 :(得分:1)

您的问题是您正在遍历Menu节点,而不是Main节点或Mods节点:

XmlNodeList Menus = xml.SelectNodes("/MenuFile/Menu");
foreach (XmlNode MenuItem in Menus) {

将始终返回外部菜单节点。

要获取Main / Mods节点,请使用以下命令:

XmlNodeList menus = xml.SelectNodes("/MenuFile/Menu/*");
foreach (XmlNode menuItem in menus) {

另外,只需直接获取“标题”和“项目”节点,而不是执行不必要的“if”:

XmlNode headerItem = menuItem.SelectSingleNode("Header");
...
XmlNodeList items = menuItem.SelectNodes("Item");
foreach (XmlNode item in items) {
   ...