Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C:使用XML文件根据用户选择填充表单字段_C#_Xml_Textbox_Populate - Fatal编程技术网

C# C:使用XML文件根据用户选择填充表单字段

C# C:使用XML文件根据用户选择填充表单字段,c#,xml,textbox,populate,C#,Xml,Textbox,Populate,在我的表单上,我设置了两个字段。项目和子区域的位置。我试图根据用户选择的位置填充子区域字段。例如,他们选择位置4。该位置仅有3个子区域,例如A、B和C。在位置框中选择后,子区域框将仅显示A、B和C。我在名为appsettings.xml的xml文件中拥有所有位置和允许的子区域。如何让程序读入xml文件并允许子区域字段仅填充有效数据?下面是如何设置xml文件的示例 <?xml version="1.0" encoding="utf-8" ?> <MerchandiseTrac

在我的表单上,我设置了两个字段。项目和子区域的位置。我试图根据用户选择的位置填充子区域字段。例如,他们选择位置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>
您在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);
        }

这包括你没有!在你的列表上有一个尾随逗号,就像你现在的第一个位置一样。如果是这样的话,您必须扩展代码以删除它。

其中包括相当多的步骤。我将使用XDocument.Load加载文档,然后需要将其绑定到UI。如果使用WPF,则ItemsControl的某些派生项将用于输入值,但您提到的文本框是值得注意的事实。在ASP.NET中更是如此,因为所有控件都需要重新创建,并在加载ViewState时提供相同的ID并重新添加到页面中,以便访问它们

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


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

感谢您的良好开端!两个问题:我不确定+locationID+应该是什么?我的代码不能用它编译。其次,如果您有//write sub-area target,我想将值直接放入sub-area框中,那么我需要做什么才能将其实现到sub-area框中呢?locationID是一个变量,它与head节点中的id属性具有相同的值。让我们澄清一下你想要什么,然后我编辑我的代码:你有两个文本框,当你在第一个框中输入04时,你想在第二个框中输入A,B,C?这是正确的!他们将从下拉框中选择一个位置,并且基于该位置,与该位置相关的子区域下拉列表中将显示一个选项列表。因此,如果他们在“位置”下拉框中选择04,子区域A、B、C将仅显示在子区域下拉框中Yea对不起,这一细节。再次感谢您的帮助!别担心。但是您应该重新构造XML文件。如果有多个值,请使用多个节点,而不是单个节点中的列表。