This documentation is archived and is not being maintained.

Walkthrough: Creating an XML Schema with the XML Designer

Visual Studio .NET 2003

In this walkthrough you will create an XML purchase order schema as part of a Windows Application project. The walkthrough will consist of three main sections:

  1. Creating a Windows Application project and adding an XML Schema.
  2. Creating a relational table:
    1. Adding and defining a new simpleType object.
    2. Adding and defining a new complexType object.
    3. Adding and defining a new Element object.
  3. Editing generated XML using the XML editor.

Creating a Windows Application and Adding an XML Schema

To create a new Windows Application project

  1. From the File menu, point to New, and then click Project to display the New Project dialog box.
  2. Depending on what language you want to use, select Visual C# Projects or Visual Basic Projects in the Project Types pane, and then select Windows Application.
  3. Name the project SampleSchema.

To add an XML Schema to the project

  • From the Project menu, choose Add New Item, then double-click the XML Schema icon in the Add New Item dialog box.

    The XML Designer appears.

Defining Simple and Complex Types

Before constructing the relational table, you will first build simple and complex type definitions that you will use to format specific elements of the purchase-order schema. The new types are built using existing XML data types, such as string and integer.

First you will define a simple type, which will be named stateCode. This simple type will be used to limit the size of a string to two characters.

To add an XML simpleType to the project

  1. If not already open, double click the XMLSchema1.xsd file to bring up the XML Designer.
  2. Click the XML Schema tab of the Toolbox and drag a simpleType onto the design surface.
  3. Change the name of the simpleType by clicking the first text box in the header and replacing simpleType1 with stateCode.
  4. Set the base type for the stateCode type by clicking the drop-down list in the header and selecting string.
  5. Navigate to the first cell in the next row by pressing the TAB key.
  6. Select facet from the drop-down list.
  7. Press TAB to go to the next cell and select length from the drop-down list.
  8. TAB to the third cell of the same row, enter the value 2.

    This requires that the value entered into the State field be two characters.

    Your stateCode should look like this in Schema view:

    ms171945.vbsimpletype(en-us,VS.71).gif

  9. Click the XML tab at the bottom left of the XML Designer to see the XML that has been added:
    <xs:simpleType name="stateCode">
       <xs:restriction base="xs:string">
          <xs:length value="2" />
       </xs:restriction>
    </xs:simpleType>
    

This stateCode simple type will be used to define the State element within the complex type you will create in the next section.

The complexType named addressType defines a set of elements that will appear in any element typed as addressType. For example, a billTo element will include information on names, addresses, and dates when its type is set to the previously defined addressType. By constructing the complex type and using it within an element, you are generating a nested relationship. For more information, see Creating Complex XML Types.

To add an XML complexType to the project

  1. Click the Schema tab of the XML Designer.
  2. Drag a complexType from the XML Schema tab of the Toolbox onto the design surface.
  3. Change complexType1 to addressType to name the type.
  4. Add an XML attribute to addressType by clicking the first cell of the first row and selecting element from the drop-down list.
  5. In the second column, change element1 to Name.
  6. In the third column, accept the default value of string.
  7. Add the following XML elements and set their names and types as follows:
    Element nameData type
    Streetstring
    Citystring
    StatestateCode
    PostalCodeinteger

    Your addressType should look like this in Schema view:

    ms171945.vbcomplextype(en-us,VS.71).gif

  8. To see the XML that has been added to your .xsd file, click the XML tab at the bottom of the designer. You will see the following XML:
    <xs:complexType name="addressType">
        <xs:sequence>
        <xs:element name="Name" type="xs:string"/>
        <xs:element name="Street" type="xs:string"/>
        <xs:element name="City" type="xs:string"/>
        <xs:element name="State" type="stateCode"/>
        <xs:element name="PostalCode" type="xs:integer"/>
        </xs:sequence>
    </xs:complexType>
    

Creating a Relational Table

When you drag the element object from the Toolbox to the design surface, you are really adding an element containing an unnamed complexType. Including the unnamed complex type defines the element to be a relational table. Additional elements can then be added under the complexType to define the relation fields (or columns). If you define one of these new elements to be a new unnamed complexType, you are creating a nested relation inside of the parent relation with its own unique columns. For details see Tables, Columns, Keys, and Constraints in XML Schemas.

Defining new unnamed complex type elements within the PurchaseOrder or Items element creates additional nesting in the schema. Within one purchase order there can be many Items, and within each Item, many additional elements (such as price, size, and so on). In the following procedure, an element Items is added to the purchaseOrder relational table and typed as an unnamed complexType. Because you are defining a new relational table, this causes a new element to appear on the design surface. Within the new items relation, adding the item element and setting its type to unnamed complexType, creates another relational table, which also appears on the design surface.

To add an XML element to the project

  1. Click the Toolbox and from the XML Schema tab drag an element object onto the design surface.
  2. Change element1 to PurchaseOrder to name the element. You can leave the data type as (PurchaseOrder).
  3. Add an element to the purchase order by clicking the first cell of the first row and selecting element from the drop-down list.
  4. Name the element shipTo and set its data type to addressType.
  5. Add the following XML elements and set their names and types as follows:
    Element nameData type
    billToaddressType
    shipDatedate
    Itemsunnamed complexType

    When you type the Item element to be anonymous, an additional element is added to the design surface, which is another relational table.

  6. In the Items element, add an element, name it Item, and set its type to Unnamed ComplexType.

    Your purchase order should look like this in Schema view:

    ms171945.vbrelationaltable(en-us,VS.71).gif

    The following XML has now been added to your .xsd file:

    <xs:element name="PurchaseOrder">
     <xs:complexType>
        <xs:sequence>
           <xs:element name="shipTo" type="addressType"/>
           <xs:element name="billTo" type="addressType"/>
           <xs:element name="shipDate" type="xs:date"/>
           <xs:element name="Items">
              <xs:complexType>
                 <xs:sequence>
                    <xs:element name="Item">
                       <xs:complexType>
                           <xs:sequence />
                       </xs:complexType>
                    </xs:element>
                 </xs:sequence>
              </xs:complexType>
           </xs:element>
         </xs:sequence>
      </xs:complexType>
    </xs:element>
    

Editing XML

You can use the XML tab of the XML Designer to edit the XML that was generated when you added elements and types to the designer surface. The XML editor features IntelliSense and statement completion. An invalid statement is tagged with a red wavy line. Mousing over the incorrect statement causes an error message to appear.

To edit XML

  1. Click the XML tab of the XML Designer to view the XML.
  2. Within the Item element, change the self-closing tag ( <xs:sequence /> ) into separate opening and closing tags ( <xs:sequence></xs:sequence> ).
  3. After the <xs:sequence> tag in the Item element, type the following:
    <xs:element name="Quantity" type="xs:integer"/>
    <xs:element name="Price" type="xs:decimal"/>
    <xs:element name="ProductID" type="xs:integer"/>
    

    You have created three new elements — Quantity, Price, and ProductID — and defined data types for each.

  4. Next type <invalid/> and note the red wavy line indicating an error. Mouse over the red wavy line to see an error message. Errors will appear in the Task List as well.
  5. Delete the <invalid/> tag to fix the error.
  6. Save the schema.

    The XML beneath the Items element should now look like the following in XML view:

    <xs:element name="Items">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="Item">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Quantity" type="xs:integer"/>
                <xs:element name="Price" type="xs:decimal"/>
                <xs:element name="ProductID" type="xs:integer"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:element>
    

See Also

Introduction to XML Schemas | Relational Data in XML Schemas | Tables, Columns, Keys, and Constraints in XML Schemas | XML Elements, Attributes, and Types| XML Schema Reference (XSD)

Show: