怎么把XML文件作为comboBox的数据原?
怎么把XML文件作为comboBox的数据原?
------解决方案--------------------public static DataSet ConvertXMLToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (
System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
}
然后ComboBox1.DataSource = myDataSet.Tables[0];
------解决方案--------------------XmlReader xr = XmlReader.Create(@ "XMLTest.xml ");
DataSet dt = new DataSet();
dt.ReadXml(xr);
IList <string> list = new List <string> ();
for(int i = 0; i < dt.Tables[0].Rows.Count; i++){
list .Add(dt.Tables[0].Rows[i][ "name "].ToString());
}
comboBox1.DataSource = list;
XML文档要求是dataset导出格式的。