我有以下的XML格式,与我和我使用.NET 2.0。
I have got below xml format with me and I am using .NET 2.0.
<?xml version="1.0" encoding="utf-8"?>
<publicationsList>
<publication tcmid="tcm:0-226-1">
<name>00 Primary Parent</name>
</publication>
<publication tcmid="tcm:0-227-1">
<name>01 Group Parent</name>
</publication>
<publication tcmid="tcm:0-228-1">
<name>02 Developer Library</name>
</publication>
<publication tcmid="tcm:0-229-1">
<name>03C Content Library</name>
</publication>
</publicationsList>
现在我想从上面的XML填充我DropDownList的,我的DropDownList的文本将名节点值和DropDownList的值将被使用的方法在C#tcmid属性值。
Now I want to populate my dropdownlist from the above XML, my dropdownlist TEXT will be "name" node value and dropdownlist VALUE will be "tcmid" attribute value using a method in C#.
请建议!!
您可以做这样的事情
使用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();
更新: 使用XmlDocument的
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();