This blog will demonstrate you, how to load XML
document in dataset.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Data;
using
System.IO;
namespace
Load_Xml_Document
{
class Program
{
public static
void Main(string[]
args)
{
// Store the XML document in a string
string xmlDoc =
@"<?xml version='1.0'?>
<countries>
<country>
<id>C101</id>
<Name>India</Name>
</country>
<country>
<id>C102</id>
<Name>USA</Name>
</country>
<country>
<id>C103</id>
<Name>UK</Name>
</country>
<country>
<id>C104</id>
<Name>France</Name>
</country>
<country>
<id>C105</id>
<Name>Japan</Name>
</country>
</countries>";
// Load this into a StringReader
StringReader sr =
new StringReader(xmlDoc);
// Create a new DataSet and read in the
XML
DataSet ds = new
DataSet();
ds.ReadXml(sr);
// Display the column names and row
data as a table
foreach (DataColumn
dc in ds.Tables[0].Columns)
{
Console.Write("{0,15}",
dc.ColumnName);
}
Console.Write("\n");
foreach (DataRow
dr in ds.Tables[0].Rows)
{
Console.WriteLine("{0,15}{1,15}",
dr[0], dr[1]);
}
Console.ReadKey();
}
}
}
Output