XMLNodeList 不检索正确的节点

本文关键字:节点 检索 XMLNodeList | 更新日期: 2023-09-27 18:30:33

我正在创建应用程序来制作现代战争3(XRPC)的Mod Menu。我正在使用XML对其进行自定义,我创建了一个示例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# 代码

        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 = "";
                }
            }
        }
    }

但问题是当尝试检索菜单列表/菜单的子节点时,它返回菜单列表/菜单本身我不知道。我试过调试,但结果是一样的,任何人都可以看到我的错误吗?

XMLNodeList 不检索正确的节点

您的问题是您正在循环浏览菜单节点,而不是主节点或模组节点:

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

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

要获取主节点/模组节点,请使用以下命令:

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) {
   ...