我正在处理数据视图。在其中,我必须显示从xml到网格视图列的列的值。我有这样的xml:-我也有一个网格视图,其中有两列"ID"one_answers"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)