读取xml并将数据绑定到下拉列表中 [英] read xml and bind the data into dropdown

查看:82
本文介绍了读取xml并将数据绑定到下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Xml文件





我想读这个并在第一个下拉列表中添加名称,

选择NameDropdown ....我想拆分运算符,并将它们绑定在另一个下拉列表中。

如果type是字符串然后显示一个文本框,如果是id,那么用户只能在其中输入数字。



i试图读取它也得到名字,但坚持我如何拆分运算符并将其绑定在下拉列表

I have Xml file


I want to read this and add the name in first dropdown ,
on selection of NameDropdown .... i want to split the operators by , and bind them in another dropdown .
and if type is string then show a text box and if id then user can enter only numbers in that .

i tried to read it too and getting names but stucked on the how i split the operator and bind them in dropdown

推荐答案

这是你如何通过键(名称) - 值来做到这一点(运算符)对。



This is how you can do this through key(name)-value(operator) pair.

XmlDocument doc = new XmlDocument();
         doc.LoadXml("xmlContent");
         XmlNodeList list = doc.GetElementsByTagName("doc");
         System.Collections.Generic.Dictionary<String, String> kv = new System.Collections.Generic.Dictionary<String, String>();
         foreach (XmlNode node in list)
         {
             kv.Add(node.Attributes["Name"].Value, node.Attributes["Operators"].Value);
         }
         string noValue = "noValue";
         string operatorString=kv.TryGetValue("ComapnyName",out noValue).ToString();





现在,操作符字符串将包含您在xml中定义的字符串,

在selectedindexchange事件中,您可以通过TryGetValue()获取所选名称的值,

可以获得具有该名称的关联运营商字符串,

拆分名称并将其绑定到运营商的下拉菜单。



noValue是默认值(如果它没有找到密钥),你必须用out关键字传递它。



Now the operator string would have the strings that you defined in xml,
on selectedindexchange event you can get the value of selected name,
through that TryGetValue() you can get string of associated operators with that name,
split the name and bind it to the operator''s drop-down.

noValue is the default value(in case it doesn''t find the key), and you have to pass it with out keyword.


我知道那个部分,但留下它你好。



这是完整的解决方案,



//代码中使用的术语



name_dropdown =名称下拉



operator_dropdown =下拉列表运营商



kv =键值对字典数据结构。拥有key = names和value =运算符



noValue =找不到给定名称时的默认字符串值



//用于拆分运算符并在operator_dropdown中将它们绑定在选择上的代码

name_dropdown中的值



Well i knew about that part, but left it on you.

Here is the complete solution,

//Terms used in the code

name_dropdown= dropdown of the names

operator_dropdown=dropdown of the operators

kv=Key-Value pair Dictionary data structure. Having key=names and value=operators

noValue=default string value when given name is not found

//Code for spliting the operator and binding them in operator_dropdown on selection
of a value in name_dropdown

public void name_dropdown_SelectedIndexChange(object sender, EventArgs e)
{
        String selected_name=name_dropdown.Text;
        //The key value pair data structure should be create global so that you can use
        it here
        String noValue="noValue";
        String operator_string=kv.TryGetValue(selected_name, out noValue);
        String[] operators=operator_string.split(',');
       
        //Enabled only once the user selects the name
        operator_dropdown.Enabled=true;
        operator_dropdown.DataSource=operators;        
}

All the operators associated with the name in xml would be added in the dropdown.


我用更简单的方式告诉你(通过索引)。





I am telling you in simpler way (through indexes).


XmlDocument doc = new XmlDocument();
      doc.LoadXml(xmlContent);
      XmlNodeList list = doc.GetElementsByTagName("doc");
      List<String> nameList = new List<string>();
      List<String> operatorList = new List<string>();
      foreach (XmlNode node in list)
      {
          nameList.Add(node.Attributes["Name"].Value);
          operatorList.Add(node.Attributes["Operators"].Value);
      }




String[] names = nameList.ToArray();
  String[] operators = operatorList.ToArray();







现在你可以将names数组绑定到下拉列表,

和selectedindexchange上你将获得索引。



运算符字符串将放在运算符数组中的相同索引中,

拆分该字符串,并将其绑定到运算符下拉列表。




Now you can bind the names array to the dropdown,
and on selectedindexchange you will get the index.

The operator string would be placed in same index in operators array,
split that string, and bind it to your operator dropdown.


这篇关于读取xml并将数据绑定到下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆