Hi all,
I've seen that many people have had this exact same problem in the past, but
no-one seems to post the answer when (if) they solve it... so here we go
again.
If I create a strongly typed dataset from an xsd schema I've created in
VS.NET 2002, instantiate and populate the dataset and write it as XML using
WriteXML() the generated XML file is not valid based on the xsd file used to
create the dataset.
When validating, the error is "The 'http://tempuri.org/Test.xsd:Test'
element is not declared. An error occurred at "... test_data.xml(2, 2)."
Here is the original xsd:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="Test" targetNamespace="http://tempuri.org/Test.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/Test.xsd"
xmlns:mstns="http://tempuri.org/Test.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="NameType">
<xs:sequence>
<xs:element name="FirstName" type="xs:string" />
<xs:element name="LastName" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="People" type="NameType"></xs:element>
</xs:schema>
Here is the code used to create and populate the dataset:
new Test().WriteXmlSchema("..\\..\\test_schema.xml");
t.People.AddPeopleRow("Joe", "Bloggs");
t.People.AddPeopleRow("Jane", "Doe");
t.WriteXml("..\\..\\test_data.xml");
And here is the generated output:
<?xml version="1.0" standalone="yes"?>
<Test>
<People>
<FirstName>Joe</FirstName>
<LastName>Bloggs</LastName>
</People>
<People>
<FirstName>Jane</FirstName>
<LastName>Doe</LastName>
</People>
</Test>
As you can see, the generated output has an additional root element - the
"Test" element.
I would have expected something like:
<?xml version="1.0" standalone="yes"?>
<People>
<FirstName>Joe</FirstName>
<LastName>Bloggs</LastName>
</People>
<People>
<FirstName>Jane</FirstName>
<LastName>Doe</LastName>
</People>
I know a lot of people have seen this before, but if someone could point me
in the right direction it'd be great! (I'll also post the answer for those
who follow!). I've obviously missed something fairly straightforward here!
I should also say I've tried using the the XMLDataDocument() clases, but I
cannot get the schema to stay the way I had expected - it works well loading
an XML file, manipulating it via the dataset and storing it again, but when
you don;t Load() an xml file, as in my case, it doesn't seem to work (even
if I try ReadXMLSchema() on the documents dataset prior to populating).
This is driving me crazy!
Thanks,
Dave.