Don't want dataset name in XML file!

N

Nick

How do I get .net to NOT put the name of the dataset as the root tag
in the XML file that is created with the dataset.WriteXML function?

I am loading my XML schema into the dataset, then loading the dataset
with data, then outputting it as an XML file, and this stupid
"<NewDataSet>" tag shows up as the root tag! Well, guess what? NOW THE
NEW FILE IS INVALID BECAUSE IT DOESN'T FOLLOW THE SCHEMA DEFINITION!

Please, someone tell me how to fix this! (Note [because of other
postings I have seen] I do NOT want to CHANGE THE NAME of this root
tag, I WANT TO REMOVE IT COMPLETELY!)

Thanks!
 
M

Miha Markic

Hi Nick,

AFAIK You can't change WriteXML behaviour.
OTOH you may change the produced XML.
 
N

Nick

So basically, does this mean that .net can't handle an XSD schema
definition? This basically makes the whole XML read and write
functionality of .net totally useless.

If I load a schema into a dataset, then output an XML file from that
dataset, the fact that .net puts this "NewDataSet" tag into the
outputed file means that THIS FILE IT CREATED IS NOT VALID AS IT DOES
NOT FOLLOW THE SCHEMA DEFINITION. (so what's the point of even being
able to load a schema definition??????)

It is just me, or does this seem totally broken? Any Microsoft people
reading this? Any plans to fix this bug?

Thanks!

---Nick


Miha Markic said:
Hi Nick,

AFAIK You can't change WriteXML behaviour.
OTOH you may change the produced XML.

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

Nick said:
How do I get .net to NOT put the name of the dataset as the root tag
in the XML file that is created with the dataset.WriteXML function?

I am loading my XML schema into the dataset, then loading the dataset
with data, then outputting it as an XML file, and this stupid
"<NewDataSet>" tag shows up as the root tag! Well, guess what? NOW THE
NEW FILE IS INVALID BECAUSE IT DOESN'T FOLLOW THE SCHEMA DEFINITION!

Please, someone tell me how to fix this! (Note [because of other
postings I have seen] I do NOT want to CHANGE THE NAME of this root
tag, I WANT TO REMOVE IT COMPLETELY!)

Thanks!
 
D

David

So basically, does this mean that .net can't handle an XSD schema
definition? This basically makes the whole XML read and write
functionality of .net totally useless.

No, it really doesn't mean that. But the question here is what does
your schema look like? XML files have root tags, Datasets naturally
have root tags, why not just define your schema to have an appropriate
root tag as well?
 
N

Nick

David said:
No, it really doesn't mean that. But the question here is what does
your schema look like? XML files have root tags, Datasets naturally
have root tags, why not just define your schema to have an appropriate
root tag as well?

Ok, here's some data. Say I have the following Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="RootTable">
<xs:complexType>
<xs:sequence>
<xs:element name="TableLevel1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="TableLevel2" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Data" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

If I read it into a dataset in .NET, fill it with data, then write it
back out, I get the following:

<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
<RootTable>
<TableLevel1>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
</TableLevel1>
<TableLevel1>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
</TableLevel1>
</RootTable>
</NewDataSet>

The root level tag defined in the XSD is ALWAYS going to become the
top level TABLE in the dataset. Since the writeXml function is ALWAYS
going to put the dataset name tag OUTSIDE the top level table, it will
ALWAYS add an extra level to the tag structure (that is not defined in
the XSD).

I can find no way around this, other then using getXML to put the XML
data into a string, and then stripping out the dataset name tags
before saving the file, which seems pretty stupid to me.

---Nick
 
D

David

Ok, here's some data. Say I have the following Schema:
If I read it into a dataset in .NET, fill it with data, then write it
back out, I get the following:

<?xml version="1.0" standalone="yes" ?>
<NewDataSet>
<RootTable>
<TableLevel1>

<snip>

The problem is that I'm just not sure what you're doing with the
dataset to fill the data. I think I'm missing something here, so
maybe some code would be the clearest way to communicate.
For example...

-

// Load the dataset with your schema (the one I snipped above)
DataSet ds = new DataSet();
ds.ReadXmlSchema("YourSchema.xsd");

DataTable dt1 = _ds.Tables["TableLevel1"];
DataTable dt2 = _ds.Tables["TableLevel2"];
DataTable dtData = _ds.Tables["Data"];

// and fill it with some data

for(int i = 1; i <= 2; i++) {
dt1.Rows.Add(new object[] { i });
for(int j = 1; j <= 2; j++) {
int id = (j * i) + j;
dt2.Rows.Add(new object[] { id, i });
for(int k = 0; k < 2; k++) {
dtData.Rows.Add(new object[] { "String", id});
}
}

}

ds.WriteXml("SomeFile.xml")



That gives me...


<?xml version="1.0" standalone="yes"?>
<RootTable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<TableLevel1>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
</TableLevel1>
<TableLevel1>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
<TableLevel2>
<Data>String</Data>
<Data>String</Data>
</TableLevel2>
</TableLevel1>
</RootTable>


I just don't see the problem.
 
N

Nick

David said:
The problem is that I'm just not sure what you're doing with the
dataset to fill the data. I think I'm missing something here, so
maybe some code would be the clearest way to communicate.
For example...

[code removed]

Ah, I see the issue now. The schema definition I posted *does* work
ok. (I tried to make a simple example, becuase my real schema is very
complex). The issue I am having arises from having a simple element in
the root node. See the following modified schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="RootTable">
<xs:complexType>
<xs:sequence>
<xs:element name="TableLevel1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="TableLevel2" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Data" type="xs:string"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Note" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Beign that element "Note" is now directly under "RootTable", ADO needs
to make a table to hold this data (it can't be just loose in the
dataset). Therefore it creates a "RootTable" data table, and sets the
name of the dataset to "NewDataSet". This causes all the tables to
basically get shifted down a notch (under the "NewDataSet" element).
This is the issue, being that the created XML (using dataset.WriteXML)
is now not valid to the schema I read in. Any way around this without
changing my schema to fit shortcommings of ADO.NET?

---Nick
 
D

David

Ah, I see the issue now. The schema definition I posted *does* work
ok. (I tried to make a simple example, becuase my real schema is very
complex). The issue I am having arises from having a simple element in
the root node. See the following modified schema:
<snip>

I didn't mean to just drop this thread (work got a bit overwhelming the
past few days), but as you've probably guessed I don't know a solution
for you either. I was kinda hoping someone would pop up with an
answer but I suspect there just isn't one.
 
Joined
Mar 15, 2006
Messages
1
Reaction score
0
I guess you'd just have to remove the unwanted root tag, using code like this:


public static string getDataSetXmlWithoutRoot (DataSet ds) {

XmlDocument doc =
new XmlDocument();

doc.LoadXml (ds.GetXml ());

return doc.FirstChild.InnerXml;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top