SgDotNet
Singapore Professional .NET User Group -For Cool Developers

XmlSchema class mapping

This post has 0 Replies | 0 Followers

Top 25 Contributor
Posts 232
Thanh Posted: 04-25-2005 7:23 AM

When there is a need to programatically manipulate the content of the schema definition file (*.xsd), the .NET library already has something in the arsenal; System.Xml.Schema namespace.

let's say that the content of our schema definition is listed below:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="books">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="book" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="author" type="xs:string" />             
              <xs:element name="title" type="xs:string" />   
            </xs:sequence>
            <xs:attribute name="publisher" type="bk:publisherType" use="required" />           
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:annotation>
    <xs:documentation xml:lang="en">
      The publisherType is a list of the publishers I've bought books from.
      If a publisher is not on the list then it means I don't have any books from them.
    </xs:documentation>
  </xs:annotation>

  <xs:simpleType name="publisherType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="WROX" />
      <xs:enumeration value="Prentice Hall" />     
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

The corresponding class that is mapped to each node is as follow:

Class                                         Node

XmlSchema                               <xs:schema> </xs:chema>
XmlSchemaElement                   <xs:element> </xs:element>
XmlSchemaComplexType          <xs:complexType> </xs:complexType>
XmlSChemaChoice                    <xs:sequence> </xs:sequence>
XmlSchemaAnnotation               <xs:annotation> </xs:annotation>
XmlSchemaDocumentation         <xs:documentation> </xs:documentation> XmlSchemaSimpleType              <xs:simpleType> </xs:simpleType>
XmlSchemaSimpleTypeRestriction <xs:restriction> </xs:restriction>
XmlSchemaEnumerationFacet      <xs:enumeration> </xs:enumeration>

As you can see that we can programatically manipulate specific node or even create the entire schema definition in code. All we have to look is inside the System.Xml.Schema namespace

Hope it helps

Page 1 of 1 (1 items) | RSS
Copyright SgDotNet 2004-2008
Powered by Community Server (Commercial Edition), by Telligent Systems