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
2009-09-07 13:52:04
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
2009-09-07 14:00:06
Sorry, lazy reading. I can use the web request stream. Sounds great!
cerhart
2009-09-07 14:01:51
A:
You can parse your XML to DataSet and get it's DataTable:
DataSet dataSet = new DataSet();
dataSet.ReadXml("input.xml", XmlReadMode.ReadSchema);
Veton
2009-09-07 13:53:38
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
2009-09-07 13:56:57