Walkthrough: Creating an XML schema with the XML designer

Source: Internet
Author: User
Tags closing tag opening and closing tags xml attribute

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 newSimpletypeObject.
    2. Adding and defining a newComplextypeObject.
    3. Adding and defining a newElementObject.
  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. FromFileMenu, pointNew, And then clickProjectTo displayNew projectDialog box.
  2. Depending on what language you want to use, selectVisual C # ProjectsOrVisual Basic ProjectsInProject typesPane, and then selectWindows Application.
  3. Name the projectSampleschema.

To add an XML schema to the Project

  • FromProjectMenu, chooseAdd new item, Then double-clickXML SchemaIcon inAdd new itemDialog 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 namedStatecode. 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 clickXmlschema1.xsdFile to bring up the XML designer.
  2. ClickXML SchemaTab Of the toolbox and dragSimpletypeOnto the design surface.
  3. Change the name of the simpletype by clicking the first text box in the header and replacingSimpletype1WithStatecode.
  4. Set the base type for the statecode type by clicking the drop-down list in the header and selectingString.
  5. Navigate to the first cell in the next row by pressing the tab key.
  6. SelectFacetFrom the drop-down list.
  7. Press tab to go to the next cell and selectLengthFrom the drop-down list.
  8. Tab to the third cell of the same row, enter the value2.

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

    Your statecode shocould look like this in schema View:

  9. ClickXMLTab at the bottom left ofXML designerTo see the XML that has been added:
    <xs:simpleType name="stateCode">   <xs:restriction base="xs:string">      <xs:length value="2" />   </xs:restriction></xs:simpleType>

ThisstateCodeSimple type will be used to defineStateElement within the complex type you will create in the next section.

The complextype namedaddressTypeDefines a set of elements that will appear in any element typed as addresstype. For example,billToElement 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. ClickSchemaTab Of the XML designer.
  2. DragComplextypeFromXML SchemaTab of the Toolbox onto the design surface.
  3. ChangeComplextype1ToAddresstypeTo name the type.
  4. Add an XML Attribute to addresstype by clicking the first cell of the first row and selectingElementFrom the drop-down list.
  5. In the second column, changeElement1ToName.
  6. In the third column, accept the default valueString.
  7. Add the following XML elements and set their names and types as follows:
    Element name Data Type
    Street String
    City String
    State Statecode
    Postalcode Integer

    Your addresstype shoshould look like this in schema View:

  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 used items, and within each item, incluadditional 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 fromXML SchemaTab dragElementObject onto the design surface.
  2. ChangeElement1ToPurchaseorderTo name the element. You can leave the data type (Purchaseorder).
  3. Add an element to the purchase order by clicking the first cell of the first row and selectingElementFrom the drop-down list.
  4. Name the elementShiptoAnd set its data typeAddresstype.
  5. Add the following XML elements and set their names and types as follows:
    Element name Data Type
    Billto Addresstype
    Shipdate Date
    Items Unnamed 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. InItems Element, Add an element, name itItem,And set its typeUnnamed complextype.

    Your purchase order shocould look like this in schema View:

    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 intelliisense 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. ClickXMLTab Of the XML designer to view the XML.
  2. WithinItemElement, change the self-closing tag (<xs:sequence />) Into separate opening and closing tags (<xs:sequence></xs:sequence>).
  3. After<xs:sequence>Tag inItemElement, 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<Invalid/>Tag to fix the error.
  6. Save the schema.

    The XML beneathItemsElement shoshould 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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.