【问题标题】:Read child node from one branch only仅从一个分支读取子节点
【发布时间】:2020-05-13 01:45:15
【问题描述】:

我查看了这里的几个示例,看起来我正在遵循正确的程序,但它仍然无法正常工作,所以我显然做错了。 我有两个组合框,我试图从 XML 文件中填充 这个想法是,一旦做出第一个选择,就会生成第二个组合框的一组选择,如果我切换第一个选择,那么第二个组合框也应该刷新

这是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<ComboBox>
    <Customer name="John"/>
        <Data>
            <System>Linux</System>
        </Data>
    <Customer name="Fernando"/>
        <Data>
            <System>Microsoft</System>
            <System>Mac</System>
        </Data>
</ComboBox>

这是我的 C# 代码

//This part works the customer_comboBox is generated correctly with John and Fernando

 XmlDocument doc = new XmlDocument();
 doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) +@"\comboBox.xml");
 XmlNodeList customerList = doc.SelectNodes("ComboBox/Customer");

 foreach (XmlNode node in customerList)
 {
     customer_comboBox.Items.Add(node.Attributes["name"].InnerText);
 }

//putting the selected item from the first comboBox in a string
string selected_customer = customer_comboBox.SelectedItem.ToString();

//This is the part that does not work
//I want to read XML node that matches selected_customer and populate systems available

foreach (XmlNode node in customerList)
{
      if (node.Attributes["name"].InnerText == selected_customer) 
      {
          foreach (XmlNode child in node.SelectNodes("ComboBox/Customer/Data"))
          {
             system_comboBox.Items.Clear();
             system_comboBox.Items.Add(node["System"].InnerText); 
          }
      } 
}

我连续两天试图弄清楚这一点。不确定我的 XML 是否错误或我调用子节点的方式。

【问题讨论】:

    标签: c# combobox xmldocument xmlnode child-nodes


    【解决方案1】:

    我检查了您的代码,并进行了以下更改。

    1。 XML 文件

    客户节点已关闭且未嵌套数据/系统节点。这会产生以下两个 xpath:

    1. 组合框/客户;
    2. 组合框/数据/系统;

    通过将数据/系统节点嵌套在客户节点中是解决方案的第一部分:

    <?xml version="1.0" encoding="utf-8" ?>
    <ComboBox>
      <Customer name="John">
        <Data>
          <System>Linux</System>
        </Data>
      </Customer>
      <Customer name="Fernando">
        <Data>
          <System>Microsoft</System>
          <System>Mac</System>
        </Data>
      </Customer>
    </ComboBox>
    

    2。代码的变化

    XML 结构的更改简化了“node.SelectNodes()”语句中的 xpath 使用,只包含“Data/System”。我还将“node[”System“].InnerText”简化为“child.InnerText”:

    foreach (XmlNode node in customerList)
    {
        if (node.Attributes["name"].InnerText == selected_customer)
        {
            system_comboBox.Items.Clear();
    
            foreach (XmlNode child in node.SelectNodes("Data/System"))
            {
                system_comboBox.Items.Add(child.InnerText);
            }
        }
    }
    

    3。删除此块

    因为客户列表需要它的项目在运行时已经存在。手动将项目添加到组合框并删除此块:

    foreach (XmlNode node in customerList)
    {
        customer_comboBox.Items.Add(node.Attributes["name"].InnerText);
    }
    

    【讨论】:

    • 谢谢!!所以我的 XML 格式不正确。它现在可以工作了,唯一的问题是当我选择“Fernando”时它只输出“Mac”它会跳过“Microsoft”
    • @Fernando 我也为你解决了这个问题。查看更新的答案。您必须将 Clear() 移到 foreach 之外,因为在向 system_combobox 添加新项目后,前一个项目将被清除。
    • 没问题@Fernando。很高兴能提供任何帮助
    • @Fernando,别忘了将我的答案标记为您问题的答案。
    猜你喜欢
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2011-02-24
    相关资源
    最近更新 更多