Home > C#, XML > DataSet.ReadXmlSchema Method Limitations

DataSet.ReadXmlSchema Method Limitations

We basically use the DataSet.ReadXmlSchema Method to create the schema which includes table, relation, and constraint definitions for the Dataset. Having said this here is something that’s worth sharing to all of my fellow developers who happen to stumble upon this post in search of a situation to find that their dataset is loading incorrect tables when your xml schema is being read!

Case 1: Sample1.xsd

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:simpleType name="PropertyNames">
    <xs:restriction base="xs:string">
      <xs:enumeration value="prop1"></xs:enumeration>
      <xs:enumeration value="prop2"></xs:enumeration>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="Items">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Item" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="properties" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

When you use the dataset’s “ReadXmlSchema” method you will find that the dataset will contain 2 tables!

// Create the DataSet to read the schema into.
DataSet ds = new DataSet();
// Invoke the ReadXmlSchema method with the file name.
ds.ReadXmlSchema(@"C:\\test\\Sample1.xsd");

A XML instance representation for this schema will be:

<Items>
  <Item> [1..*]
    <name> xs:string </name> [1]
    <properties> xs:string </properties> [0..*]
  </Item>
</Items>

Case 2: Sample2.xsd

<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:simpleType name="PropertyNames">
    <xs:restriction base="xs:string">
      <xs:enumeration value="prop1"></xs:enumeration>
      <xs:enumeration value="prop2"></xs:enumeration>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="Items">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Item" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string"/>
              <xs:element name="properties" type="PropertyNames" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This time when you execute the following code you will find that the dataset will contain 1 table!

// Create the DataSet to read the schema into.
DataSet ds = new DataSet();
// Invoke the ReadXmlSchema method with the file name.
ds.ReadXmlSchema(@"C:\\test\\Sample1.xsd");

Whereas the XML instance representation for this schema will be:

<Items>
  <Item> [1..*]
    <name> xs:string </name> [1]
    <properties> PropertyNames </properties> [0..*]
  </Item>
</Items>

Actually, you should have noted that thou the XML instance representation for this schema (Case 2) is similar to the one in (Case 1), the dataset had only one table instead of two; After several hours of investigation and read through, I happen to discover (atleast lucky) thats there is a “Microsoft Support Article” which explains the “Limitations for DataSet Schema Files (XSD)”.

Please find the references to the file at the following location: http://support.microsoft.com/kb/319372

Question: How does this article applies to my scenario?
Answer: Use of Restriction Element Is Mostly Ignored
You can derive a new simple type by restricting an existing simple type with the restriction element. When you use the restriction element in simple types, the restriction element is ignored. Therefore, all the sub-elements of that restriction element are ignored also.

All restriction elements are ignored except the XSD simple type “string”, and its facets as follows:
> length
> minlength
> maxlength

Advertisements
  1. May 27, 2011 at 8:35 AM

    Thanks, very usefull post.

  2. August 15, 2011 at 5:52 PM

    Thank you for another fantastic post. Where else could anybody get that kind of information in such a perfect way of writing? I’ve a presentation next week, and I’m on the look for such info.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: