Home All Groups Group Topic Archive Search About

DataSet changes schema of XML file.

Author
28 Apr 2006 2:21 AM
MaLec
I’d like to use the DataSet as the container for the XML files conforming to
some schema. The problem is, that the operation of reading the XML file into
the DataSet having its schema and then writing it back to the file makes it
invalid, not obeying rules of this schema.
Here is the simple test program performing this operation.

------------------------------------------------------------------------
using System;
using System.Data;
namespace DataSetTest
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet DS = new DataSet();
            DS.ReadXmlSchema ("XMLSchema1.xsd");
            DS.ReadXml("XmlFile1.xml", XmlReadMode.IgnoreSchema);
            DS.WriteXml("XmlFile2.xml", XmlWriteMode.IgnoreSchema);
        }
    }
}
------------------------------------------------------------------------

The schema is contained in the XMLSchema1.xsd file:

------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XMLSchema1"
targetNamespace="http://tempuri.org/XMLSchema1.xsd"
elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
          <xs:element name="A" type="ct" />
          <xs:element name="B" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="ct">
    <xs:attribute name="attribute1" type="xs:string" />
  </xs:complexType>
</xs:schema>
------------------------------------------------------------------------

Here is the valid XML file XMLFile1.xml:

------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns="http://tempuri.org/XMLSchema1.xsd">
  <A attribute1 ="att"/>
  <B>some text</B>
</Root>
------------------------------------------------------------------------

After transformation we get the XMLFile2.xml, which is invalid:

------------------------------------------------------------------------
<?xml version="1.0" standalone="yes"?>
<XMLSchema1 xmlns="http://tempuri.org/XMLSchema1.xsd">
  <Root>
    <B>some text</B>
    <A attribute1="att" />
  </Root>
</XMLSchema1>
------------------------------------------------------------------------

The XML is invalid because:
1. The enclosing XMLSchema1 element is added, which is not included in the
schema. This is not difficult to correct this by simple post processing, but
it would be more elegant, if there was a way of avoiding this.
2. The elements A and B have changed order although the schema organizes
them in the sequence structure. This happens, when sequence contains
intermixed simple and complex elements. WriteXml method always outputs simple
elements before complex, disregarding schema. I could not find any trivial
method of correcting this. It seems that the information about the order is
lost in the DataSet for such case.

I’ll be obliged for suggestions.

AddThis Social Bookmark Button