[英]Any way to populate items from xml to Datagridview using c#
我正在研究数据网格视图。 在其中我必须显示从 xml 到网格视图列的列的值。 我有像这样的 xml:- 我还有一个网格视图,它有两列“ID”和“NAME”,我想将 xml 中的值填充到网格视图。有人可以帮忙吗?
<employee>
<empdetails id="1" name="sam"/>
<empdetails id="2" name="robin"/>
<empdetails id="3" name="victor"/>
</employee>
您可以将 xml 读取到 DataSet 并将 DataSet empdetails
表传递给 DataGridView,如下所示:
//Create xml reader
XmlReader xmlFile = XmlReader.Create("fullPathToYourXmlFile.xml", new XmlReaderSettings());
DataSet dataSet = new DataSet();
//Read xml to dataset
dataSet.ReadXml(xmlFile);
//Pass empdetails table to datagridview datasource
dataGridView.DataSource = dataSet.Tables["empdetails"];
//Close xml reader
xmlFile.Close();
您可以使用 XML Linq 如下
XElement xml = XElement.Load(XMl String);
var xmlData = from item in xml.Element("empdetails")
select new {id = item.Attribute("id") , name= item.Attribute("name")};
dataGrid.DataSource = xmlData.ToList();
C#
DataSet ds = new DataSet();
ds.ReadXml("C:/XMLData/employee.xml");
DataGridView1.DataSource = ds.Tables(0);
VB.NET
Dim ds As New DataSet
ds.ReadXml("C:/XMLData/employee.xml")
DataGridView1.DataSource = ds.Tables(0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.