Convert XML Stream to DataSet

  • Thread starter Thread starter Brad
  • Start date Start date
B

Brad

I am trying to convert an XML Stream received from a web api call into
a DataSet to use in the rest of the app. The issue I am running into
is that it will not convert the stream to a dataset and halts the
program.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

WebClient client = new WebClient();
string url="<url of the api>";
Stream data = client.OpenRead(url);
StreamReader reader = new StreamReader(data);

DataSet ds = new DataSet();
ds.ReadXml(data); //This is where the program fails.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I am using Framework 1.1 still (if that helps.)
 
The error message I get is:

An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: The same table (link) cannot be the child
table in two nexted relations.
 
Using strings to create XML is a HORRIBLE PRACTICE:

Here is some code:

private DataSet GetDataSet1()
{
DataSet ds = new DataSet();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<?xml version=\"1.0\"?><items>");
sb.Append("<item>");
sb.Append("<key>abc</key>");
sb.Append("<value>Apple Berry Cat</value>");
sb.Append("<time>" + DateTime.Now.ToLongTimeString() + "</time>");
sb.Append("</item>");
sb.Append("<item>");
sb.Append("<key>def</key>");
sb.Append("<value>Dough Elephant Fence</value>");
sb.Append("<time>" + DateTime.Now.ToLongTimeString() + "</time>");
sb.Append("</item>");
sb.Append("<item>");
sb.Append("<key>hij</key>");
sb.Append("<value>House Igloo Jumprope</value>");
sb.Append("<time>" + DateTime.Now.ToLongTimeString() + "</time>");
sb.Append("</item>");
sb.Append("</items>");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(sb.ToString());
writer.Flush();
ms.Position = 0;
ds.ReadXml(ms);
return ds;
}

I use this for some light testing, but NEVER FOR REAL/PRODUCTION CODE.

Buyer beware.
 
Argument Null helps a lot!

Be sure to initialize your objects:

Stream data = new Stream(); // Stream may take parameters
data = client.OpenRead(url);

Then, keep going!
 
I believe the error is occurring because you have more than one containing
xml nodes in the XML that have the same name. the DataSet is trying to figure
out which nodes should become datatables, and that's why its failing. you
could try some of the overloads "fragment", "inferschema" etc.

Otherwise, you are going to have to fix up your xml. By the way, you can
load the xml directly into the DataSet by passing the url into the ReadXml
method. There is no need for the WebClient code.
-- Peter
To be a success, arm yourself with the tools you need and learn how to use
them.

Site: http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://ittyurl.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top