tags:

views:

346

answers:

3

This is something I will be able to do, but I'm curious to hear people's ideas about the best way to do it. I have an XML file sitting on the web at http://www.someplace.com/file and I am writing a web service that will take that data and convert it to a DataTable object, then return the datatable. We're using c# 3.5. What do you think is the best way to tackle this?

+2  A: 

Simply download the XML file to your local disk, and then create the DataTable, and call DataTable.ReadXml(filename) on it..... or am I missing something....

The DataTable.ReadXml even supports a stream - so you could hook it up directly to your WebResponse stream from downloading the XML from that URL.

(this is untested, from memory - but should give you an idea how to approach this):

DataTable myDataTable = new DataTable();

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(new Uri("http://someplace/somefile.xml");
myRequest.Method = "GET";

WebResponse myResponse;
try
{
   myResponse = myRequest.GetResponse();

   using (Stream responseStream = myResponse.GetResponseStream())
   { 
      myDataTable.ReadXml(responseStream);
   }
}
catch
{ }

Marc

marc_s
I don't want the IO cost, is my only option to loop through and XML document in memory and manually add it to the data table?
cerhart
Sorry, lazy reading. I can use the web request stream. Sounds great!
cerhart
+2  A: 
DataTable dt = new DataTable();
dt.ReadXml("c:myxml.xml");
MusiGenesis
A: 

You can parse your XML to DataSet and get it's DataTable:

DataSet dataSet = new DataSet();
dataSet.ReadXml("input.xml", XmlReadMode.ReadSchema);
Veton
Actually, the XML sits outside my domain, so I would have to save it to the disk, and then load it. I was hoping to avoid the IO cost there.
cerhart