To create an XML file with an associated XML schema

Source: Internet
Author: User
Tags add definition end include string valid zip visual studio
xml| Create | schema

First, create an XML file with an associated XML schema
1. 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," from the File menu, point to New, 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 Types pane, and then select Windows Application. Name the project "Xmlcustomerlist", and then click OK to create the project.

2, Visual Studio adds the Xmlcustomerlist project to Solution Explorer.

Add a new XML file entry to the project and choose Add New Item from the Project menu. The Add New Item dialog box appears. Select XML file from the templates area of the Add New Item dialog box. Name the XML file "CustomerList", and then click Open.

3. Add new XML schema items to the project

Add a new XML schema item to the project and choose 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 then click Open.

4. Add a simple type definition to the schema

Create a simple Type element that represents a 5-bit 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 on 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 a simple type cannot contain elements or attributes as part of its content model. Only facet can be used to generate simple types. Use the TAB key to navigate to the next cell on the right, and select pattern from the Drop-down list. Use the TAB key again to navigate to the next cell on the right, and type "\d{5}".

The pattern facet allows you to enter regular expressions. The regular Expression \d{5} represents a 5-bit limit on the content of the "Zip Code" (postalCode) type. Regular expressions are beyond the scope of this walkthrough, but you can see how to use pattern facet with the selected data type to allow only specific data in a simple type.

If you switch the schema to an XML view, you should see the following code in the root-level schema tag (which means that the code example does not include the actual declaration portion of the frame, nor does it include the actual schema markup called the root or document level markup):

<xs:simpletype name= "PostalCode" >

<xs:restriction base= "Xs:positiveinteger" >

<xs:pattern value= "\d{5}"/>

</xs:restriction>

</xs:simpleType>

From the File menu, choose Save All.

5. Adding complex type definitions to schemas

Create a complex Type element that represents a standard U.S. 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 the Drop-down list box to see multiple element options that you can add to a complex type. You can select elements, but for the remainder of this walkthrough, you will simply TAB through the cell, because the element is the default setting. Use the TAB key to navigate to the next cell on the right, and type Name.

Use the TAB key to navigate to the next cell on the right and set the data type to string. Repeat creates a new row in the USAddress element for the following items:

Element name
Data type

Street
String

City
String

State
String

Zip
PostalCode

Note The data type that is assigned to the zip element. It is the PostalCode simple type you created previously.

If you switch to an XML view, you should see the following code in the root-level schema tag (which means that the code example does not include the actual declaration portion of the schema, nor does it include the actual schema markup called the root or document level markup):

<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 that you have defined two individual types, they can be used for element definitions and types. From the File menu, choose Save All. To add a primary element to a 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 the 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. From the XML Schema tab of the Toolbox, drag an element to 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 line, and type "CompanyName". Use the TAB key to navigate to the next cell on the right and set the data type to string. Repeat creates a new row in the Customer element for the following items:

Element name
Data type

ContactName
String

Email
String

Phone
String

Billtoaddress
USAddress

Shiptoaddress
USAddress

Note the data types that are assigned to the billing address (billtoaddress) element and the shipping address (shiptoaddress) element. It is the USAddress complex type that was previously created. We may have defined simple types for email, phone number, and so on.

If you switch the schema to an XML view, you should see the following code in the root-level schema tag (which means that the code example does not include the actual declaration portion of the frame, nor does it include the actual schema markup called the root or document level markup):

<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>

From the File menu, choose Save All.

7. In order to allow multiple instances of customer data within an XML document, we will create an element named 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 (previously created) and drag it onto the customerlist element. A separate design pane is bound to represent the hierarchical structure of the data. From the File menu, choose Save All.

8, the schema and XML files associated

Create an association between an XML file and an XML Schema in 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 a <customerList> tag.

Adding data to an XML file

9, you can now add data to the XML file. By associating a schema with an XML file, the XML editor now knows the valid elements that can be included in the XML file and provides a formatted grid in the Data view.

Add data to the CustomerList.xml file, and position the cursor at the start and end <customerList> tags in the "customerlist.xml" file in the XML view (start tag = <customerlist Between the end 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. In the grid, in the Contact Name field, type Nate Sun. Fill in the records by adding data to the other fields in the grid. Cut back to the XML view. The data in the grid is now correctly formatted as XML.

Ii. creating XML schemas from XML files

Create a new XML schema based on an existing XML document

1. Load an XML document (. xml file) into the XML Designer.

2, from the "XML" menu click "Create Schema."

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 that are assigned when the schema is created.

Note When you derive a schema from an existing XML document, all data types start with string, so you must edit the data type based on the content 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.



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.