Vinit Patel

Read XML document into DataTable

Posted on: October 1, 2009


// <summary>
/// method for reading an XML file into a DataTable
/// </summary>
/// <param name=”file”>name (and path) of the XML file</param>
/// <returns></returns>
public DataTable ReadXML(string file)
{
//create the DataTable that will hold the data
DataTable table = new DataTable(“XmlData”);
try
{
//open the file using a Stream
using(Stream stream = new  FileStream(file, FileMode.Open, FileAccess.Read))
{
//create the table with the appropriate column names
table.Columns.Add(“Name”, typeof(string));
table.Columns.Add(“Power”, typeof(int));
table.Columns.Add(“Location”, typeof(string));

//use ReadXml to read the XML stream
table.ReadXml(stream);

//return the results
return table;
}
}
catch (Exception ex)
{
return table;
}
}

The other useful link is,

http://www.codeproject.com/Messages/3094681/XML-to-DataTable.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Calendar

October 2009
M T W T F S S
« Sep   Dec »
 1234
567891011
12131415161718
19202122232425
262728293031  

Stats Of Blog

  • 6,824 hits

Cluster Map

%d bloggers like this: