1. Create an XML file with an associated XML schema
1. To create a new Windows application project,
you first need to create a new Windows application in Visual Basic or Visual C#. Create a new project and name it "XMLCustomerList", point to New from the File menu, and then click Project to display the New Project dialog box. Depending on the language you want to use, select Visual Basic Project or Visual C# Project in the Project Type pane, and then select Windows Application. Name the project "XMLCustomerList" and click OK to create the project.
2. Visual Studio will add the XMLCustomerList project to the Solution Explorer.
To add a new XML file item to the project, select Add New Item from the Project menu. The Add New Item dialog box will appear. Select XML File from the Templates area of the Add New Item dialog box. Name the XML file "CustomerList" and click Open.
3. Add a new XML schema item to the project.
To add a new XML schema item to the project, select "Add New Item" from the "Project" menu. The Add New Item dialog box appears. Select XML Schema from the Templates area of the Add New Item dialog box. Name the schema "CustomerListSchema" and click Open.
4. Add a simple type definition to the schema.
Create a simple type element that represents a 5-digit postal code.
From the "XML Schema" tab of the "Toolbox", drag a "simpleType" onto the design surface. Select the default name "simpleType1" and rename the type to "postalCode". Use the TAB key to navigate to the next cell to the right and select "positiveInteger" from the drop-down list. Use the TAB key to navigate to the next line.
Click the drop-down box. The only option is facet. This is because simple types cannot contain elements or attributes as part of their content model. Only facets can be used to generate simple types. Use the TAB key to navigate to the next cell to the right and select "pattern" from the drop-down list. Use the TAB key again to navigate to the next cell to the right and type "d{5}".
Pattern facets allow you to enter regular expressions. The regular expression d{5} means that the content of the "postalCode" type is limited to 5 characters. Regular expressions are beyond the scope of this walkthrough, but you can see how to use pattern facets with selected data types to allow only specific data in simple types.
If you switch the schema to XML view, you should see the following code in the root-level schema tag (this means that the code example includes neither the actual declaration part of the framework nor the actual schema tag called the root or document-level tag) :
<xs:simpleType name="postalCode">
<xs:restriction base="xs:positiveInteger">
<xs:pattern value="d{5}" />
</xs:restriction>
</xs:simpleType>
Select Save All from the File menu.
5. Add a complex type definition to the schema
Create a complex type element that represents a standard US address
Switch to Schema view. From the XML Schema tab of the Toolbox, drag a complexType onto the design surface. Select the default name "complexType1" and rename the type to "usAddress". Do not select a data type for this element. Use the TAB key to navigate to the next line. Click on the drop-down list box and you will see several options for elements that can be added to the complex type. The element can be selected, but for the rest of this walkthrough, you will just TAB over the cell because the element is set by default. Use the TAB key to navigate to the next cell to the right and type "Name".
Use the TAB key to navigate to the next cell to the right and set the data type to string. Repeat in the usAddress element to create a new row for:
element name
data type
Street
string
City
string
State
string
Zip
postalCode
Note the data type 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 in the root-level schema tag (this means that the code example includes neither the actual declaration part of the schema nor the actual schema tags called 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>
Now you have defined two separate types that can be used in element definitions as well as types. Select Save All from the File menu. Add the main element to the schema
6. After defining some data types, you can construct the actual data definition for the XML file that will be created. The XML file will contain data for the customer list, so create the actual elements that define the data that will be valid in the XML file.
Create the Customer element
Switch to Schema view. Drag an "element" from the XML Schema tab of the Toolbox onto the design surface. Select the default name "element1" and rename it to "customer". Do not select a data type for this element. Use the TAB key to navigate to the center cell of the next row and type "CompanyName". Use the TAB key to navigate to the next cell to the right and set the data type to string. Repeat to create new rows in the Customer element for:
element name
data type
ContactName
string
Email
string
Phone
string
BillToAddress
usAddress
ShipToAddress
usAddress
Note the data types assigned to the "BillToAddress" element and the "ShipToAddress" element. It is the previously created usAddress complex type. We might have defined simple types for the Email, Phone elements, etc.
If you switch the schema to XML view, you should see the following code in the root-level schema tag (this means that the code example includes neither the actual declaration part of the framework nor the actual schema tag called the root or document-level tag) :
<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>
Select Save All from the File menu.
7. To allow multiple instances of customer data within the XML document, we will create an element called customerList that will contain all the individual customer elements.
Create the customerList element Drag an "element" from the XML Schema tab of the Toolbox onto the design surface. Select the default name "element1" and rename it to "customerList". Do not select a data type for this element. Select the customer element (created previously) and drag it onto the customerList element. Separate design panes are bound to represent the hierarchical structure of the data. Select Save All from the File menu.
8. Associate the schema with the XML file.
Create an association between the XML file and the XML schema. In the "Solution Explorer", double-click the "CustomerList.xml" file. The XML file opens in the designer's XML view. 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> tag.
Adding data to the XML file
9. Now you can add data to the XML file. By associating a schema with an XML file, the XML editor now knows the valid elements to include in the XML file and provides a formatted grid in the data view.
To add data to the customerList.xml file, in the "customerList.xml" file in XML view, position the cursor between the opening and closing <customerList> tags (starting tag = <customerList>, closing tag = </customerList>) .
Type <. Select the Customer element.
Type > to end the tag.
Type < and select CompanyName from the list of valid elements.
Type > to end the tag.
Type Blue Yonder Airlines as the company name.
Switch to Data view. Type Nate Sun in the Contact Name field in the grid. Fill out records by adding data to other fields in the grid. Switch back to "XML" view. Data in the grid is now correctly formatted as XML.
2. Create an XML schema from an XML file.
Create a new XML schema based on an existing XML document.
1. Load an XML document (.xml file) into the "XML Designer".
2. Click Create Schema from the XML menu.
3. An XML schema (.xsd file) will be added to the current project with the same name as the original XML file.
4. Load the newly created XML schema (.xsd file) into the "XML Designer".
5. Verify and edit the data types assigned when creating the schema.
Note When deriving a schema from an existing XML document, all data types are initially set to string, so you must edit the data types according to the content requirements of the XML data.
If you need to make changes to the schema, you can use the XML Designer to add, edit, and remove elements.
http://www.cnblogs.com/xh831213/archive/2006/09/14/503944.html