Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 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..Frown | :(
please help me..
thank you in advance
god bless u all..Smile | :)
Posted 21-Feb-13 21:04pm
MAU7871.6K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

http://csharpdotnetfreak.blogspot.com/2012/06/read-xml-file-into-datatable-aspnet-cvb.html[^]
 
create datatable ... and than bind dropdown using this datatable... Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
Smile | :)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100