By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

Convert XML Stream to DataSet

P: n/a
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.)
Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jun 27 '08 #2

P: n/a

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.


"Brad" <mi******@gmail.comwrote in message
news:39**********************************@j22g2000 hsf.googlegroups.com...
>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.)

Jun 27 '08 #3

P: n/a
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!

"Brad" wrote:
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.)
Jun 27 '08 #4

P: n/a
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
"Brad" wrote:
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.
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.