Load XML into a Dataset [C#/.NET]

If you’re working with XML files in .NET, the easiest thing to do is load the XML in as a DataTable:

using System.Data.SqlClient;

public DataTable ReturnDataTbl(string XmlFile)
  {
  DataSet dset = new DataSet();

  FileStream fstr = new FileStream(XmlFile, FileMode.Open, FileAccess.Read);
  dset.readXml(fstr);

  DataTable dtbl = dset.Tables[0];

  fstr.Close();
  dset.Dispose();

  return dtbl;
  }

You can now run a SELECT, UPDATE or any SQL statement on it directly.


4 Comments

DaTomAugust 18th, 2004 at 6:25 pm

could you please tell me how sql-querrys on datasets work?

thanks in advance

JohanSeptember 2nd, 2004 at 9:35 pm

Hi,

I’m wondering how to excecute the sql statement on the datatable? Could you please show us how or which method / property to use?

Thank you,
Johan

shaneSeptember 29th, 2005 at 11:33 am

If you specify the schema the XML file is parsed and ready much faster. This may only really apply to big XML files though.

Shane

Javier M.July 20th, 2007 at 4:20 pm

Thanks for your code lines …

Leave a comment

Your comment