Converting XML to DataTable
This is something that I can do, but I am curious to hear people ideas on how to do this. I have an XML file sitting on the internet at http://www.someplace.com/file and I am writing a web service that will take this data and transform it into a DataTable Object and then return the data. We are using C # 3.5. What do you think is the best way to handle this?
+2
source to share
3 answers
Just load the XML file to your local drive and then create a DataTable and call DataTable.ReadXml (filename) on it ..... or am I missing something ....
The DataTable.ReadXml is stream-aware, so you can connect it directly to the WebResponse stream by loading 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
{ }
Mark
+4
source to share