Populating dropdown from the XML in C#

后端 未结 5 2145
花落未央
花落未央 2021-01-06 06:00

I have got below xml format with me and I am using .NET 2.0.



  

        
5条回答
  •  没有蜡笔的小新
    2021-01-06 06:27

    You could do something like this

    Using Linq

    XDocument xDoc = XDocument.Load(@"Yourxmlfile.xml");
                var query = from xEle in xDoc.Descendants("publication")
                            select new ListItem(xEle.Element("name").Value, xEle.Attribute("tcmid").Value);
    
                ddlList.DataValueField = "value";
                ddlList.DataTextField = "text";
                ddlList.DataSource = query;
                ddlList.DataBind();
    

    Update: Using XmlDocument

    XmlDocument xDocument = new XmlDocument();
                xDocument.Load(@"YourXmlFile.xmll");
                foreach (XmlNode node in xDocument.GetElementsByTagName("publication"))
                {
                    ddlList.Items.Add(new ListItem(node.SelectSingleNode("name").InnerText,
                        node.Attributes["tcmid"].Value));
                }
                ddlList.DataValueField = "value";
                ddlList.DataTextField = "text";            
                ddlList.DataBind();
    

提交回复
热议问题