471,049 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

DataSet changes schema of XML file

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 was 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.

Apr 28 '06 #1
2 1562
What version of the framework are you using?
"MaLec" <Ma***@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
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 was 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.

May 11 '06 #2
Environment.Version.ToString() returns: 2.0.50727.42

"Zafar Abbas" wrote:
What version of the framework are you using?
"MaLec" <Ma***@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
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 was 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.


May 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Wil | last post: by
1 post views Thread by Patrik | last post: by
4 posts views Thread by Brian Keating | last post: by
4 posts views Thread by Sindarian | last post: by
4 posts views Thread by Robert Bravery | last post: by
4 posts views Thread by Al | last post: by
1 post views Thread by GoogleGroups | last post: by

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.