473,804 Members | 3,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to load DataTable into XMLDocument?

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

Thanks,
Brett

Dec 15 '05 #1
6 28488
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(s w);
DataSet ds = new DataSet("Test") ;
ds.WriteXml(xtr , XmlWriteMode.Wr iteSchema);
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(d t);

System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
XmlTextWriter xmltw = new XmlTextWriter(s w);

XmlDocument xmlDoc = new XmlDocument();
ds.WriteXml(xml tw, XmlWriteMode.Ig noreSchema);
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
6825
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. The problem is that I'm getting multiple processing instructions and multiple root elements in my xml file when I use a FileStream. Here is an example: test_1.xml contains: <?xml version="1.0" encoding="utf-8"?> <root>
1
2515
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 = CredentialCache.DefaultCredentials // Create and load the XmlDocument XmlDocument doc = new XmlDocument() doc.XmlResolver = resolver; // Set the resolver doc.Load(@"\\devsrv\profiles\0004.xml") OR..
2
6045
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 XmlDocument TransformXmlDoc(XmlDocument docToTransform, string xsltFilePath) { //load the xslt
1
9699
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
2506
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 corresponding menu IDs. Some pages have the same menu IDs. One every page load, I want to check the name, and get the menu ID so the menu can be displayed with the proper items highlighted. The list various from a couple of dozen entries to a...
0
1567
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 eternity for you. Works like a charm. (I know, I know, I love you too) Sean
4
3616
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 can be passed, which are XML.
0
1425
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"); but when I attempt to access it, I receive a "Additional information: System error." message. I tried the XmlTextReader , but have the same issue. The only reader that appears to work is the XmlValidatingReader.
10
2180
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 mcF2ToXml(. . .) As Xml.XmlDocument . . .
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10320
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.