creating DropdownList

后端 未结 2 1405
刺人心
刺人心 2021-01-29 02:08

Consider the following XML file:

   
            
              
             


        
相关标签:
2条回答
  • 2021-01-29 02:15

    How about this?

            XmlDocument doc = new XmlDocument();
            doc.Load(@"~/XML/XML.xml");
            var nodes = doc.SelectNodes("warehouse/cat[@id='computer']/item/SN");
    
            myDropDown.DataTextField = "InnerText";
            myDropDown.DataValueField = "InnerText";
    
    
            //now bind the dropdownlist to the dataview
            myDropDown.DataSource = nodes;
            myDropDown.DataBind();
    
    0 讨论(0)
  • 2021-01-29 02:35

    Load the xml into an XElement:

            var xml = XElement.Load("test.xml");
    

    Execute XPath to select the SN elements in the cats with id computer: (+ put them in a list)

            var snValues = xml.XPathSelectElements("//cat[@id='computer']/item/SN")
                            .Select(x => x.Value).ToList();
    

    Required usings:

            using System.Linq;
            using System.Xml.Linq;
            using System.Xml.XPath;
    
    0 讨论(0)
提交回复
热议问题