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:
- Creating a Windows application project and adding an XML schema.
- Creating a relational table:
- Adding and defining a newSimpletypeObject.
- Adding and defining a newComplextypeObject.
- Adding and defining a newElementObject.
- Editing generated XML using the XML editor.
Creating a Windows application and adding an XML schema
To create a new windows application project
- FromFileMenu, pointNew, And then clickProjectTo displayNew projectDialog box.
- Depending on what language you want to use, selectVisual C # ProjectsOrVisual Basic ProjectsInProject typesPane, and then selectWindows Application.
- Name the projectSampleschema.
To add an XML schema to the Project
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
- If not already open, double clickXmlschema1.xsdFile to bring up the XML designer.
- ClickXML SchemaTab Of the toolbox and dragSimpletypeOnto the design surface.
- Change the name of the simpletype by clicking the first text box in the header and replacingSimpletype1WithStatecode.
- Set the base type for the statecode type by clicking the drop-down list in the header and selectingString.
- Navigate to the first cell in the next row by pressing the tab key.
- SelectFacetFrom the drop-down list.
- Press tab to go to the next cell and selectLengthFrom the drop-down list.
- 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:
- 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>
ThisstateCode
Simple type will be used to defineState
Element within the complex type you will create in the next section.
The complextype namedaddressType
Defines a set of elements that will appear in any element typed as addresstype. For example,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
- ClickSchemaTab Of the XML designer.
- DragComplextypeFromXML SchemaTab of the Toolbox onto the design surface.
- ChangeComplextype1ToAddresstypeTo name the type.
- Add an XML Attribute to addresstype by clicking the first cell of the first row and selectingElementFrom the drop-down list.
- In the second column, changeElement1ToName.
- In the third column, accept the default valueString.
- 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:
- 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
- Click the toolbox and fromXML SchemaTab dragElementObject onto the design surface.
- ChangeElement1ToPurchaseorderTo name the element. You can leave the data type (Purchaseorder).
- Add an element to the purchase order by clicking the first cell of the first row and selectingElementFrom the drop-down list.
- Name the elementShiptoAnd set its data typeAddresstype.
- 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.
- 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
- ClickXMLTab Of the XML designer to view the XML.
- Within
Item
Element, change the self-closing tag (<xs:sequence />
) Into separate opening and closing tags (<xs:sequence></xs:sequence>
).
- After
<xs:sequence>
Tag inItem
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.
- 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.
- Delete<Invalid/>Tag to fix the error.
- Save the schema.
The XML beneathItems
Element 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>