473,385 Members | 1,397 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,385 software developers and data experts.

how to: DataSet -> XML using tablename as root element

This seems like it should be so trivial, but try as I might I can't figure it out.

I want to write out a dataset as xml (i.e., DataSet.WriteXml()) but use the tablename as the root element instead of the datasetname. For example, if I have:

private void simpleDataSetXml()
{
DataSet ds = new DataSet();
DataTable table = new DataTable("foo");
DataColumn col = new DataColumn("bar", System.Type.GetType("System.String"));
table.Columns.Add(col);

DataRow row = table.NewRow();
row["bar"] = "baz";

table.Rows.Add(row);
ds.Tables.Add(table);

StringWriter writer = new StringWriter();
ds.WriteXml(writer);
System.Console.WriteLine(writer.ToString());
}

What I'd like to see is:

<foo>
<bar>baz</bar>
</foo>

Instead of:

<NewDataSet>
<foo>
<bar>baz</bar>
</foo>
</NewDataSet>

I know that it's somehow possible because if I do a DataSet.ReadXml() of the following:

<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>baz</bar>
</foo>

And then do a DataSet.WriteXml() then it gives me exactly what I want. When I compare the dataset structures in the debugger the only difference I see between my programmatically-generated dataset and the one created from the XML is that the dataset created from XML has:

fTopLevelTable = true

Whereas the programmatically generated one has fTopLevelTable = false.

Now fTopLevelTable is a private variable so I can't just modify it, but I can't for the life of me figure out what property to set or method to call to get it to be true.

Any help would be greatly appreciated.

-Dave
Dec 12 '06 #1
4 4993
try this
DataSet ds = new DataSet("foo");
Dec 12 '06 #2
try this
DataSet ds = new DataSet("foo");
That will give me

<foo>
<foo>
<bar>baz</bar>
</foo>
</foo>
Dec 12 '06 #3
obviously some thing will come in the place of <NewDataSet>
Dec 13 '06 #4
obviously some thing will come in the place of <NewDataSet>
No, not obviously. And that's exactly what I want (nothing to replace NewDataSet). I want the table to be the root element, not the dataset. This is what happens if you read in xml using DataSet.ReadXml(). As mentioned earlier, if you do a ReadXml() on:

<foo>
<bar>baz</bar>
</foo>

It will create a dataset with a datasetname of NewDataSet and a table named foo. If you do a DataSet.WriteXml() on that same dataset it will generate:

<foo>
<bar>baz</bar>
</foo>

Not

<NewDataSet>
<foo>
<bar>baz</bar>
</foo>
</NewDataSet>

This is exactly what I want. What I want to figure out is how to create programmatically what the internals of ReadXml() seem to be doing. And as I said, if you look at the 2 datasets in the debugger (i.e., the one created doing a ReadXml() and the one using the code I wrote above) the only difference between the 2 is that the one created by ReadXml() has fTopLevelTable = true. Everything else is identical, including the datasetname = NewDataSet, so I'm guessing it's that flag that's causing the difference, but I don't know what I need to do to cause that flag to be set true.
Dec 13 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Mike | last post by:
I need to expand the DataSet class by inheriting from it and adding functions that work on the data in the tables. However, since I can't upcast how can I get my base DataSet object assigned an...
10
by: localhost | last post by:
I have a static, thread-safe class with a DataSet as a property. When the class is instanced into an object, some callers add rows to a DataTable in the DataSet. Other callers read the DataSet. ...
5
by: Christian Ahrenkiel | last post by:
Hi, I've got a DataSet (ds) which was created by a DataAdapter (da) method: ds.Fill(ds). This DataSet is passed through various methods (by value). Each method changes some fields in the...
3
by: JJ | last post by:
Hi, I need to pass a dataset to another win form along with a SqldataAdapter. I don't want to recreate the SqlDataAdapter again either. So to pass to another Win form in my windows form app, do...
5
by: Jason | last post by:
I am having problems understanding how to access a datasource only once, fill a single dataset, and then reference that dataset multiple times through different user controls(ascx) found on the...
22
by: EMW | last post by:
Hi, I managed to create a SQL server database and a table in it. The table is empty and that brings me to my next chalenge: How can I get the info in the table in the dataset to go in an empty...
4
by: Robert Bravery | last post by:
Hi All, I'm New to VS.net. Trying to figure out this new dataset thingy. Ok so I add a dataadapter with all the correct properties for a table. Then generate the dataset. Now open the dataset and...
1
by: Optimus | last post by:
Hi everyone, I currently develop an application in vs.net 2005 with vb.net. I was trying to use typed dataset and I've got in trouble for converting untyped dataset into Typed DataSet. I don't...
4
by: Justin | last post by:
I'm just learning ASP.NET 2.0 and everything I find on Datasets including the MSPress book I bought go way to far for what I need. I do not need to bind a datasource. My source will be a text...
1
by: matt | last post by:
hello, i have a web app that allows users to query our oracle db and produce a dataset of report data. they then have the option to serialize this data and store it in the database. later, then...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.