Any way to populate elements from xml in Datagridview using C #

I am working on a datagridview. In which I have to show the column values โ€‹โ€‹from xml to the grid view column. I have xml: In addition, I have a grid view that has two columns "ID" and "NAME", I want to fill in the values โ€‹โ€‹from the xml view into the grid. Can anyone help?

<employee> <empdetails id="1" name="sam"/> <empdetails id="2" name="robin"/> <empdetails id="3" name="victor"/> </employee> 
+4
source share
3 answers

You can read the xml in the DataSet and pass the empdetails DataSet to the DataGridView as follows:

 //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(); 
+11
source

You can use XML Linq as below

 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(); 
+3
source
 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) 
-1
source

All Articles