Click here to Skip to main content
15,881,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI all,
i have this xml document

XML
<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 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..:)
Posted

Hi

just i modified your code and make it according to your need...
C#
{
            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
:-)
 
Share this answer
 
http://csharpdotnetfreak.blogspot.com/2012/06/read-xml-file-into-datatable-aspnet-cvb.html[^]

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900