<?xml version="1.0" encoding="UTF-8" standalone="no"?> <info> <id>001</id> <name>xyz</name> <salary>1000</salary> <phone>1234567890</phone> </info>
using System.Xml; using System.IO; namespace XML_Reader { public partial class Form1 : Form { string[] fileArray; string directory_path = "C:\\Users\\XYZ\\Desktop\\test\\"; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Add columns to empty datagridview dataGridView1.Columns.Add("id", "id"); dataGridView1.Columns.Add("name", "name"); dataGridView1.Columns.Add("salary", "salary"); dataGridView1.Columns.Add("phone", "phone"); populateRecords(); } private void populateRecords() { DataSet ds; DataGridViewRow dg_row; //Read all XML files and records to datagrid fileArray = Directory.GetFiles(directory_path, "MyFiles*.xml"); foreach (string xmlFile in fileArray_collection) { //Read the XML from the file ds = new DataSet(); ds.ReadXml(xmlFile); //create new row for datagrid dg_row = (DataGridViewRow)dataGridView1.Rows[0].Clone(); //assign values to cells in the new row dg_row.Cells[0].Value = ds.Tables["info"].Rows[0]["id"]; dg_row.Cells[1].Value = ds.Tables["info"].Rows[0]["name"]; dg_row.Cells[2].Value = ds.Tables["info"].Rows[0]["salary"]; dg_row.Cells[3].Value = ds.Tables["info"].Rows[0]["phone"]; //Add the new row to datagrid -- THE MOMENT RECORD IS DETECTED dataGridView1.Rows.Add(dg_row); dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1;//This will keep the last added row visible with vertical scrollbar being at bottom. } } } }
WEB : http://www.aspsnippets.com/Articles/Read-XML-File-and-bind-it-to-GridView-in-ASPNet.aspx Windows : http://www.codeproject.com/Tips/426762/XML-to-DataSet-or-GridView-and-back
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)