我有一个像这样的xml文件:
<DATASET>
<RECORD ClientId="1398" Name="Mausami Pandit"></RECORD>
<RECORD ClientId="1121" Name="Tony Mead"></RECORD>
<RECORD ClientId="1124" Name="Frank Lead"></RECORD>
<RECORD ClientId="1008" Name="Julie Lily"></RECORD>
</DATASET>
我需要将此xml数据用于c#.net。
中的下拉菜单 string s13 = GetClientXML(); // by this function i am taking xml data to s13 variable.
StringReader theReader = new StringReader(s13);
DataSet theDataSet = new DataSet();
DataRow row1 = theDataSet.Tables[0].NewRow();
row1["ClientId"] = 0;
row1["Name"] = "-- Select --";
theDataSet.Tables[0].Rows.Add(row1);
theDataSet.ReadXml(theReader);
// ddlassto is my combobox. System.Windows.Forms.ComboBox
ddlassto.DataSource = theDataSet.Tables[0];
ddlassto.ValueMember = "ClientId";
ddlassto.DisplayMember = "Name";
但这不起作用。它没有填入下拉列表。
有谁可以帮我解决这个问题?
答案 0 :(得分:0)
将xml存储在数据集中,然后将其分配给dropdownlist ..
DataSet ds=new DataSet();
ds.ReadXml("xmlfile.xml");
ddlassto.DataSource = ds; or ddlassto.DataSource = ds.Tables[0];
ddlassto.TextField = "field name"; // field to display in dropdown
ddlassto.ValueField="Value Field";
ddlassto.DataBind();
答案 1 :(得分:0)
将一个逐个值添加到下拉列表项的另一种方法如果我从评论中了解您想要将xml值添加到列表项的问题,请尝试以下
XmlDocument xdoc=new XmlDocument();
xdoc.Load("xmlfile.xml");
XmlNodeList node = xdoc.SelectSingleNodes("/NewDataSet/resources/");
foreach(XmlNode n in node )
{
ListItem l = new ListItem();
l.Text = n.InnerXml.ToString();
ddlassto.Items.Add(l);
}
ddlassto.DataBind();
答案 2 :(得分:0)
var items = XElement.Parse(GetClientXML()).Descendants()
.Select(node=> new{ClientId =(string)node.Attribute("ClientId"), Name = (string)node.Attribute("Name")}).ToList();
ddlassto.DataSource = items;
ddlassto.ValueMember = "ClientId";
ddlassto.DisplayMember = "Name";