将xml数据源绑定到下拉列表 [英] Bind xml datasource to dropdownlist

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

问题描述

HI all,

i有这个xml文件



HI all,
i have this xml document

<RolesList>
  <Roles ID="01">
    <Role>
      <text>HO Admin</text>
      <value>1</value>
    </Role>
    <Role>
      <text>Circle Admin</text>
      <value>2</value>
      </Role>
    <Role>
      <text> Branch Admin</text>
      <value>3</value>
     </Role>
  </Roles>
  <Roles ID="02">
    <Role>
      <text> Branch Admin</text>
      <value>3</value>
    </Role>
  </Roles>
</RolesList>





i希望将此xml绑定到我的下拉列表

但是我无法绑定它

我亲自检查一下loged的角色

然后根据它选择一个节点然后我想要将该节点绑定到下拉列表

这里是我的代码:







i want to bind this xml to my dropdown list
but i m not able to bind it
firt i check for role of loged in person
and then select a node basing on that and then i want to bind that node to dropdownlist
here is my code:


XmlDataSource oXmlDataSource = new XmlDataSource();
            oXmlDataSource.XPath = "siteMap/siteMapNode";
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("~/Web/sitemap/LoadRole.xml"));
            XmlNode root = doc.DocumentElement;
            XmlNodeList nodeList = doc.SelectNodes("/RolesList/Roles[@ID=" + UserRole + "]");
            drpUpdateRole.Items.Insert(0, new ListItem(string.Empty, string.Empty));
            List<ListItem> items = new List<ListItem>();

            foreach (XmlNode node in nodeList)
            {
                 for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                     
                    items.Add(new ListItem(node.ChildNodes[i].Attributes["text"].Value, node.ChildNodes[i].Attributes["value"].Value));
             
            }
 
            }
            drpUpdateRole.Items.AddRange(items.ToArray());







但它无法正常工作.. :(

请帮帮我..

提前谢谢

上帝保佑你们所有..:)




but it is not working..:(
please help me..
thank you in advance
god bless u all..:)

推荐答案

http://csharpdotnetfreak.blogspot.com/2012/06/read-xml-file-into-datatable-aspnet-cvb.html [ ^ ]



创建数据表...而不是使用此数据表绑定下拉列表...:)
http://csharpdotnetfreak.blogspot.com/2012/06/read-xml-file-into-datatable-aspnet-cvb.html[^]

create datatable ... and than bind dropdown using this datatable... :)






只是我修改了你的代码并根据你的需要制作......

Hi

just i modified your code and make it according to your need...
{
            XmlDataSource oXmlDataSource = new XmlDataSource();
            oXmlDataSource.XPath = "siteMap/siteMapNode";
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(".") + "\\LoadRole.xml");
            XmlNode root = doc.DocumentElement;
            //  XmlNodeList nodeList = doc.SelectNodes("/RolesList/Roles[@ID=" + UserRole + "]");
            XmlNodeList nodeList = doc.SelectNodes("/RolesList/Roles[@ID=01]");

            drpUpdateRole.Items.Insert(0, new ListItem(string.Empty, string.Empty));
            List<ListItem> items = new List<ListItem>();

            foreach (XmlNode node in nodeList)
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                {
                    string text = node.ChildNodes[i].InnerXml;
                    string xml = @"<Test>" + text + "</Test>";
                    XDocument doc2 = XDocument.Parse(xml);
                    //Get your wood nodes and values in a list 
                    List<Tuple<string, string>> list = doc2.Descendants().Select(a => new Tuple<string, string>(a.Name.LocalName, a.Value)).ToList();
                    items.Add(new ListItem(list[1].Item2, list[2].Item2));

                }

            }
            drpUpdateRole.Items.AddRange(items.ToArray());
        }





jmd

: - )



jmd
:-)


http://r4r.co.in/asp.net/01/tutorial/asp.net/How%20to%20populate%20combobox%20from%20xml%20file%20using%20c-Sharp%20in %20asp.net.shtml [ ^ ]


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

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