Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Creating an XML File with an Associated XML Schema

Visual Studio .NET 2003

This walkthrough will illustrate the capabilities of working with XML files that have associated schemas. It will take you through the process of creating an XML file and an XML Schema based on the XML Schema definition language (XSD). Then you will create an association between the XML Schema and the XML file. After this association is established, you will work on the XML file in XML view in order to see the advanced capabilities of the XML editor. For this walkthrough the XML file will represent a list of companies. You will see how the XML file contains the data whereas the XML Schema only defines that data.

Note   By associating an XML Schema with your XML file, statement completion is enabled in the XML editor.

Creating a New Windows Application Project

To begin this walkthrough you will first need to create a new Windows Application in Visual Basic or Visual C#.

To create a new project and name it "XMLCustomerList"

  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 Basic Projects or Visual C# Projects in the Project Types pane, and then select Windows Application.
  3. Name the project XMLCustomerList, then click OK to create the project.

    Visual Studio will add the XMLCustomerList project to Solution Explorer.

Add a New XML File item to your Project

Because this walkthrough requires an XML file, you need to add one to your project.

To add a new XML file item to the project

  1. From the Project menu, select Add New Item.

    The Add New Item dialog box appears.

  2. In the Templates area of the Add New Item dialog box, select XML File.
  3. Name the XML file CustomerList, and then click Open.

    Visual Studio will add a file called CustomerList.xml to the project and automatically load it into the designer.

Add a New XML Schema item to your Project

Because this walkthrough requires an XML Schema, you need to add one to your project.

To add a new XML Schema item to the project

  1. From the Project menu, select Add New Item.

    The Add New Item dialog appears.

  2. In the Templates area of the Add New Item dialog box select XML Schema.
  3. Name the schema CustomerListSchema and then click Open.

    Visual Studio will add a file called CustomerListSchema.xsd to the project and automatically load it into the designer.

Add a Simple Type Definition to your Schema

Now you need to define the elements that will contain the data in your XML file. Because an XML Schema defines the data in an associated XML file, you create the element definitions in the schema.

The first definition will be a simple type element that will be used to define a standard US postal code (ZIP code). For this walkthrough we will use 5-digit codes only.

To create a simple type element that represents a 5 digit postal code

  1. From the XML Schema tab of the Toolbox, drag a simpleType onto the design surface.
  2. Select the default name simpleType1 and rename this type to postalCode.
  3. Use the TAB key to navigate one cell to the right and select positiveInteger from the drop-down list.
  4. Use the TAB key to navigate to the next row.
  5. Click the drop-down box.

    The only choice is facet. This is because simple types cannot include elements or attributes as part of their content models. Only facets can be used to build simple types.

  6. Use the TAB key to navigate one cell to the right and select pattern from the drop-down list.
  7. TAB over one cell to the right again and type \d{5}.

    The pattern facet allows you to enter regular expressions. The regular expression \d{5} says that the contents of the postalCode type is restricted to 5 digits. Regular expressions are beyond the scope of this walkthrough, but you can see how you can use the pattern facet in conjunction with the chosen data type to only allow specific data in a simple type.

    If you switch your schema to XML view you should see the following code within the root level schema tags (that means the code sample does not include the actual declaration part of the schema, nor does it include the actual schema tags which are called the root or document level tags):

       <xs:simpleType name="postalCode">
          <xs:restriction base="xs:positiveInteger">
             <xs:pattern value="\d{5}" />
          </xs:restriction>
       </xs:simpleType>
    
  8. From the File menu, select Save All.

Add a Complex Type Definition to your Schema

The next definition will be a complex type element that will be used to define an address. As part of this complex type definition, we will use the simple type created in the previous steps.

To create a complex type element that represents a standard US address

  1. Switch back to Schema view.
  2. From the XML Schema tab of the Toolbox, drag a complexType onto the design surface.
  3. Select the default name complexType1 and rename this type to usAddress. Do not select a data type for this element.
  4. Using the TAB key, navigate to the next row.
  5. Click the drop-down list box to see the many choices of elements you can add to a complex type. You can select element, but for the rest of the walkthrough you will just TAB over this cell because element is the default.
  6. Using the TAB key, navigate one cell to the right and type Name.
  7. TAB one cell to the right and set the data type to string.
  8. Repeat Steps 4 – 7 and create new rows in the usAddress element for the following:
    Element nameData type
    Streetstring
    Citystring
    Statestring
    ZippostalCode

    Notice the data type that is assigned to the Zip element. It is the postalCode simple type you created previously.

    If you switch to XML view, you should see the following code within the root level schema tags (that means the code sample does not include the actual declaration part of the schema, nor does it include the actual schema tags which are called the root or document level tags):

       <xs:simpleType name="postalCode">
          <xs:restriction base="xs:positiveInteger">
             <xs:pattern value="\d{5}" />
          </xs:restriction>
       </xs:simpleType>
       <xs:complexType name="usAddress">
          <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="xs:string" />
             <xs:element name="Zip" type="postalCode" />
          </xs:sequence>
       </xs:complexType>
    

    You have now defined two separate types that can be used in element definitions as well as types.

  9. From the File menu, select Save All.

Add the Main Elements to your Schema

Now that you have defined some data types lets construct the actual data definition for the XML file you will be creating. The XML file will contain the data for the customer list, so lets create the actual element that will define the data that will be valid in the XML file.

To create the Customer element

  1. Switch to Schema view.
  2. From the XML Schema tab of the Toolbox, drag an element onto the design surface.
  3. Select the default name element1 and rename this to customer. Do not select a data type for this element.
  4. Using the TAB key navigate to the center cell of the next row, and type CompanyName.
  5. TAB one cell to the right and set the data type to string.
  6. Repeat Steps 4 – 5 and create new rows in the Customer element for the following:
    Element nameData type
    ContactNamestring
    Emailstring
    Phonestring
    BillToAddressusAddress
    ShipToAddressusAddress

    Notice the data type that is assigned to the BillToAddress as well as the ShipToAddress elements. It is the usAddress complex type created previously. We could have defined simple types for the Email, Phone elements and so on.

    If you switch your schema to XML view, you should see the following code within the root-level schema tags (that means the code sample does not include the actual declaration part of the schema, nor does it include the actual schema tags which are called the root or document level tags):

       <xs:simpleType name="postalCode">
          <xs:restriction base="xs:positiveInteger">
             <xs:pattern value="\d{5}" />
          </xs:restriction>
       </xs:simpleType>
       <xs:complexType name="usAddress">
          <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="xs:string" />
             <xs:element name="Zip" type="postalCode" />
          </xs:sequence>
       </xs:complexType>
       <xs:element name="Customer">
          <xs:complexType>
             <xs:sequence>
                <xs:element name="CompanyName" type="xs:string" />
                <xs:element name="ContactName" type="xs:string" />
                <xs:element name="Email" type="xs:string" />
                <xs:element name="Phone" type="xs:string" />
                <xs:element name="ShipToAddress" type="usAddress" />
                <xs:element name="BillToAddress" type="usAddress" />
             </xs:sequence>
          </xs:complexType>
       </xs:element>
    
  7. From the File menu, select Save All.

In order to allow multiple instances of customer data within the XML document, we will create an element named customerList that will contain all the individual customer elements.

To create the customerList element

  1. From the XML Schema tab of the Toolbox, drag an element onto the design surface.
  2. Select the default name element1 and rename this to customerList. Do not select a data type for this element.
  3. Select the customer element (created previously) and drag it onto the customerList element.

    The individual design grids bind to represent the hierarchical structure of the data.

  4. From the File menu, select Save All.

Associating the Schema and the XML file

In this part of the walkthrough, you will start working with the XML file. Within the XML file you will add a reference to the schema you just created.

To create an association between the XML file and the XML Schema

  1. In Solution Explorer double-click the CustomerList.xml file.
  2. The XML file opens in the designer in XML view.
  3. In the Properties window, click the cell to the right of the targetSchema property and select http://tempuri.org/CustomerListSchema.xsd.

    Visual Studio adds a reference to the schema in the CustomerList.xml file and adds the <customerList> tags.

Adding Data to your XML file

The XML file is now ready for data. By associating a schema with your XML file, the XML editor now is aware of the valid elements that can be included in your XML file, as well as providing a fromatted grid in data view.

To add data to the customerList.xml file

  1. In XML view of the customerList.xml file, position the cursor between the opening and closing <customerList> tags (Opening tag = <customerList> Closing tag = </customerList>).
  2. Type <. Select the Customer element.
  3. Type > to close the tag.
  4. Type <, and select CompanyName from the list of valid elements.
  5. Type > to close the tag.
  6. Type Blue Yonder Airlines as the company name.
  7. Switch to Data view.
  8. Type Nate Sun in the ContactName field in the grid.
  9. Complete the record by adding data into the other fields in the grid.
  10. Switch back to XML view.

    The data from the grid is now properly formatted as XML.

Next Steps

Some common next steps include:

  • Add some data in XML view and then switch to Data view. Explore the different ways to add data to your XML files using the XML Designer.
  • After adding some data, validate your document.
  • Generate a dataset from your schema.

See Also

XML Schemas and Data | XML Files | Creating New XML Files | Assigning Schemas to XML Files | Checking for Well-Formed and Valid XML | XML Elements, Attributes, and Types | Creating ADO.NET Typed DataSets from Schemas

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft