我有一个这样的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";但这不管用。它没有填充到下拉列表中。
有人能帮我解决这个问题吗?
发布于 2013-10-01 07:12:37
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";发布于 2013-10-01 07:06:19
将xml存储在数据集中,然后将其分配给下拉列表。
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();发布于 2013-10-01 07:07:23
如果我从您想要添加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();https://stackoverflow.com/questions/19109956
复制相似问题