469,312 Members | 2,496 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

How to load DataTable into XMLDocument?

I have a DataTable with one column. How can I load that into an
XMLDocument object? XMLDocument.Load() or LoadXML() will not take a
DataTable.

Thanks,
Brett

Dec 15 '05 #1
6 27895
Check out the XmlTextReader and XmlTextWriter classes.
All the samples you need are in MSDN documentation.

1 possible approach is:

Add your data table to a dataset.
Write your dataset to a XML string
Use the XmlDocument's LoadXML method.

Steven Nagy

Dec 15 '05 #2
The write your data to XML part is where I'm having trouble. All the
MSDN documentation shows how to writer to a file. That's fine but I'm
not writing to a file. I need the DataSet XML to finally end up in a
string. XmlWriter and TextWriter are both abstract classes.

Thanks,
Brett

Dec 15 '05 #3

Yes those 2 are abstract, but thats not the classes I mentioned to
begin with.
What do you get if you cross an 'XmlWriter' and a 'TextWriter'?
An 'XmlTextWriter' !
StringWriter sw = new StringWriter();
XmlTextWriter xtr = new XmlTextWriter(sw);
DataSet ds = new DataSet("Test");
ds.WriteXml(xtr, XmlWriteMode.WriteSchema);
string xml = sw.ToString();
I think this code is right. if not, its close to right.
I'm kind of in CE.NET mode at the moment.

Steven Nagy

Dec 15 '05 #4
Thanks. That got it but it's only writing the DataSet name. I'm using
IgnoreSchema at the moment since I don't need it. Here's the code:

DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("myid");
ds.Tables.Add(dt);

System.IO.StringWriter sw = new System.IO.StringWriter();
XmlTextWriter xmltw = new XmlTextWriter(sw);

XmlDocument xmlDoc = new XmlDocument();
ds.WriteXml(xmltw, XmlWriteMode.IgnoreSchema);
string xml = sw.ToString();

I want it to write this:
<myid>1</myid><myid>2</myid>...

Is that possible?

Thanks,
Brett

Dec 15 '05 #5
Well not quite. The XML it writes will be something like this:

<MyDataSet>
<MyTable>
<MyID>1</MyID>
</MyTable>
<MyTable>
<MyID>2</MyID>
</MyTable>
</MyDataSet>

You obviously haven't added any data in the above code, so all it will
write is the data set name... because its empty!
If you need the XML written a specific way, you'll have to do it
manually. And also, this means that a dataset can't be constructed
again by reading in that custom XML.

You can also use the XML schema tool that comes as a part of the
Dataset. When you add a new item to your project, an option is the
dataset. You can then construct your schema in a more manual way either
diagramtically or in XML. I'm sure this tool has a proper name and can
be called from windows directly, I just don't know what it is.

Hope this rambling has been at least partially useful.

Steven Nagy

Dec 15 '05 #6
Got it! It's working great! Thanks.

Brett

Dec 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Graham Pengelly | last post: by
reply views Thread by David | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.