Read XML file as DataSet Read XML file as DataSet xml xml

Read XML file as DataSet


If you want to use a DataSet, it is very simple.

// Here your xml filestring xmlFile = "Data.xml";DataSet dataSet = new DataSet();dataSet.ReadXml(xmlFile, XmlReadMode.InferSchema);// Then display informations to testforeach (DataTable table in dataSet.Tables){    Console.WriteLine(table);    for (int i = 0; i < table.Columns.Count; ++i)        Console.Write("\t" + table.Columns[i].ColumnName.Substring(0, Math.Min(6, table.Columns[i].ColumnName.Length)));    Console.WriteLine();    foreach (var row in table.AsEnumerable())    {        for (int i = 0; i < table.Columns.Count; ++i)        {            Console.Write("\t" + row[i]);        }        Console.WriteLine();    }}

If you want something faster, you can try with XmlReader which read line after line. But it is a bit more difficult to develop.You can see it here : http://msdn.microsoft.com/library/cc189056(v=vs.95).aspx


Other simple method is using "ReadXml" inbuilt method.

string filePath = "D:\\Self Practice\\Sol1\\Sol1\\Information.xml";DataSet ds = new DataSet();ds.ReadXml(filePath);

Note: XML file should be orderly manner.

Reference