0

在我的表单上,我设置了 2 个字段。项目的位置和子区域。我正在尝试根据用户选择的位置填充子区域字段。例如,他们选择“位置 4”。该位置仅有的 3 个子区域是 A、B 和 C。在位置框中选择后,子区域框将仅显示 A、B 和 C。我拥有所有位置并允许名为 appsettings.xml 的 xml 文件中的子区域。如何让程序读取 xml 文件并允许子区域字段仅填充有效数据?下面是我如何设置 xml 文件的示例。

<?xml version="1.0" encoding="utf-8" ?> 
 <MerchandiseTrack>

   <Merchandise_Zones Application_Data="Test-Run">

      <KeyBoard_App>
        <AppString>c windows osk.exe</AppString> 
      </KeyBoard_App>

 <Storage_Location>

     <head id="Location">                // Name of box on app
       <body id="04">                    // Name of Location within the box
         <Sub-Area>A, B, C,</Sub-Area>   // Allowed sub-areas
       </body>
     </head>

     <head id="Location">                // Name of box on app  
      <body id="05">                     //Name of Location within the box
         <Sub-Area>P, L, R, B</Sub-Area> // Allowed sub-areas 
      </body>
     </head>

     <head id="Location">                // Name of box on app
      <body id="14">                     //Name of Location within the box
       <Sub-Area>A, X, C </Sub-Area>     //Name of Location within the box
      </body>
    </head>

  </Storage_Location>
 </Merchandise_Zones>
</MerchandiseTrack>
4

2 回答 2

1

您在 上设置了一个事件SelectedIndexChanged。然后读取 locationID 并从文件中选择节点:

        XmlDocument doc = new XmlDocument();
        doc.Load(@"path/to/file.xml");
        XmlNode subarea = doc.SelectSingleNode("/MerchandiseTrack/Merchandise_Zones/Storage_Location/head/body[@id=" + locationComboBox.SelectedItem.ToString()+ "]/Sub-Area");
        string[] areas = subarea.InnerText.Split(',');
        foreach (string area in areas)
        {
           subAreaComboBox.Items.Add(area);
        }

这包括您的列表中没有(!)尾随逗号(就像您目前的第一个位置一样。如果是这样,您必须扩展代码以将其删除。

于 2012-08-06T12:22:22.267 回答
0

涉及相当多的步骤。我会XDocument.Load用来加载文档,然后你需要将它绑定到 UI。如果使用 WPF,ItemsControl 的一些派生项来输入值,但您提到 TextBoxes 的事实是需要谨慎的。在 ASP.NET 中更是如此,因为所有控件都需要重新创建,给定相同的 ID 并在加载 ViewState 时重新添加到页面中,以便您访问它们。

当您绑定/重新创建它们时,关键是确保所有 ID/Name 属性保持不变。

我个人的经验一直是 WPF 中的动态控件很麻烦,但在 ASP.NET 中很难正常工作。

于 2012-08-06T12:23:39.750 回答