473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 28391
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
1
by: Dave | last post by:
Can you load an XmlDocument with a file located on a network share? I thought maybe the XmlResolver could be used XmlUrlResolver resolver = new XmlUrlResolver() resolver.Credentials =...
2
by: Graham Pengelly | last post by:
Hi I am trying to transform on System.Xml.XmlDocument into another using XslTransform without writing the object out to a file. I am guessing it should work something like this... public...
1
by: Peter Nofelt | last post by:
Hey All, I'm running into this issue with parsing through an xml document by tag name. Below is an example xml document: File Name: things.xml <things> <people> <name>Peter</name>
3
by: Bob | last post by:
I have a name-value pair type of data that I need to search against. It's to determine what menu item to highlight given the asp.net page name. So the list has all my page names (unique) and the...
0
by: delphiconsultingguy | last post by:
Hi all, Spent WAAAYYY too much time trying to figure this out because there's not many good examples out there, so in the interest of sparing y'all from suff'rin same, I've pasted it into...
4
by: William | last post by:
I don't get it? Of all things, shouldn't an XmlDocument type be serializable by default? How can I pass this type through a remoting service? It just doesn't make sense, especially since DataSets...
0
by: David | last post by:
This is a .net 1.1 question. I am attempting to load and process an XML file (see below) with XmlDocument. I can load the file: XmlDocument doc = new XmlDocument(); doc.Load("dump.xml"); ...
10
by: Atara | last post by:
Suppose I have the following functions: Public Function myF1ToStream(...) As System.IO.MemoryStream . . . Dim ms As New System.IO.MemoryStream(buf) Return ms End Function Public Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.