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

DataSet.ReadXml with an extract from an xml file

P: n/a
Hi,

I have an xml file that encapsulates a dataset definition within a set
of tags (<dataset>)... here is an example

<?xml version="1.0" encoding="utf-16"?>
<dataset>
<MyTable>
<Field1>100</Field1>
<Field2>200</Field2>
</MyTable>
</dataset>

I first use an XmlTextReader to read the root tag and if it is the
<dataset> tag then I use DataSet.XmlRead to read into the dataset.
myDataset.ReadXml(reader);

The above example works fine and creates a single table named MyTable in
the dataset. However, if I want to add an attribute to the dataset tag:

<?xml version="1.0" encoding="utf-16"?>
<dataset myAttribute = "1">
<MyTable>
<Field1>100</Field1>
<Field2>200</Field2>
</MyTable>
</dataset>
I first use an XmlTextReader to read the root tag and if it is the
<dataset> tag then I read the attributes:

while (reader.MoveToNextAttribute())
{
}

and then I try to use

myDataset.XmlRead(reader);

The dataset is created with two tables, one for the dataset tag and the
other for MyTable.

I tried using GetRemainder afeter reading the attributes and using this
reader to fill the dataset:

XmlTextReader readerDataset = new XmlTextReader(reader.GetRemainder());
myDataset.XmlRead(readerDataset);

but this produces a parsing error because of the closing <dataset> tag.

Is there a simple way to extract the dataset xml to use in
DataSet.XmlRead?

Thanks,

Jeronimo Bertran
Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Jeronimo,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to ignore the attributes in
the dataset table and put the inner xml to a DataTable. If there is any
misunderstanding, please feel free to let me know.

In this case, I think you can try to use ReadStartElement to advance to the
next node and read the whole table into DataSet. Here is a code snippet.
HTH.

XmlTextReader r = new XmlTextReader(@"c:\b.xml");
r.MoveToContent();
r.ReadStartElement("dataset");
DataSet ds = new DataSet();
ds.ReadXml(r);
this.dataGrid1.DataSource = ds;

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #2

P: n/a
Thanks.... that worked!

Jeronimo
Nov 12 '05 #3

P: n/a
You're welcome, Jeronimo.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.