Create a Windows application in Visual Basic or Visual C. Create a new project and name it "XMLCustomerList". point to "new" from the "file" menu, and click "project" to display the "new project" dialog box. 1. create an xml file with an associated XML architecture
1. create a new Windows application project
Create a Windows application in Visual Basic or Visual C. Create a new project and name it "XMLCustomerList". point to "new" from the "file" menu, and click "project" to display the "new project" dialog box. Select Visual Basic project or Visual C # Project in the project type pane based on the language you want to use, and then select Windows application ". Name the project "XMLCustomerList" and click "OK" to create the project.
2. Visual Studio adds the XMLCustomerList project to solution resource manager.
Add a new XML file item to the project, and select "Add new item" from the "project" menu ". The "add new item" dialog box appears. Select "XML file" from the "template" area in the "add new project" dialog box ". Name the XML file "mermerlist" and click "open ".
3. add a new XML schema item to the project
Add a new XML schema item to the project, and select "Add New Project" from the "project" menu ". The "add new project" dialog box appears. Select "XML schema" from the "template" area in the "add new project" dialog box ". Name the schema "mermerlistschema" and click "open ".
4. add a simple type definition to the architecture
Create a simple type element that represents a 5-bit postal code
Drag a simpleType from the XML Schema tab of the toolbox to the design surface. Select the default name "simpleType1" and rename this type to "postalCode ". Use the TAB key to locate the next cell on the right and select positiveInteger from the drop-down list ". Use the TAB key to locate the next row.
Click the drop-down list. 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 locate the next cell on the right and select "pattern" from the drop-down list ". Use the TAB key again to locate the next cell on the right and type "\ d {5 }".
Mode facet allows you to enter a regular expression. The regular expression \ d {5} indicates that the content of the postalCode type is limited to five characters. The regular expression is beyond the scope of this walkthrough, but you can see how to use the mode facet with the selected data type so that only specific data is allowed in a simple type.
If you switch the schema to the XML view, you should see the following code in the root-level schema tag (this means that the sample code does not include the actual declaration part of the framework, it does not include actual architecture tags known as root or document-level tags ):
Select "save all" from the "file" menu ".
5. add a complex type definition to the architecture
Create a complex type element that represents a standard American address
Switch to the "architecture" view. Drag a complexType from the XML Schema tab of the toolbox to the design surface. Select the default name "complexType1" and rename this type to "usAddress ". Do not select a data type for this element. Use the TAB key to locate the next row. Click the drop-down list box and you will see multiple element options that can be added to complex types. You can select an element, but for the rest of this exercise, you will only use the TAB key to skip this cell, because the element is set by default. Use the TAB key to locate the next cell on the right and type "Name ".
Use the TAB key to locate the next cell on the right and set the data type to string. Repeat the usAddress element to create a new line for the following items:
Element name
Data type
Street string City string State string Zip postalCode
Pay attention to the data type assigned to the Zip element. It is a simple postalCode type you created previously.
If you switch to the XML view, you should see the following code in the root-level architecture tag (this means that the code example does not include the actual declaration part of the schema, it does not include actual architecture tags known as root or document-level tags ):
Now you have defined two separate types for element definition and type. Select "save all" from the "file" menu ". Add the primary element to the architecture
6. After defining some data types, you can construct an actual data definition for the created XML file. The XML file contains the data in the customer list. therefore, create an actual element that defines the data that will be valid in the XML file.
Create Customer element
Switch to the "architecture" view. Drag an element from the XML Schema tab of the toolbox to the design surface. Select the default name "element1" and rename it "customer ". Do not select a data type for this element. Use the TAB key to locate the center cell of the next row and type "CompanyName ". Use the TAB key to locate the next cell on the right and set the data type to string. Repeat the Customer element to create a new line for the following items:
Element name
Data type
ContactName string Email string Phone string BillToAddress usAddress ShipToAddress usAddress
Pay attention to the data type assigned to the BillToAddress element and ShipToAddress element. It is a complex type of usAddress that was previously created. We may have defined simple types for "Email" and "Phone number" elements.
If you switch the schema to the XML view, you should see the following code in the root-level schema tag (this means that the sample code does not include the actual declaration part of the framework, it does not include actual architecture tags known as root or document-level tags ):
Select "save all" from the "file" menu ".
7. to allow multiple instances of customer data in the XML document, we will create an element named mermerlist, which will contain all individual customer elements.
Create the customerList element and drag an "element" from the "XML schema" tab of the "toolbox" to the design surface. Select the default name "element1" and rename it "mermerlist ". Do not select a data type for this element. Select the customer element (previously created) and drag it to the customerList element. A separate design pane is bound to indicate the layered structure of the data. Select "save all" from the "file" menu ".
8. associate the architecture with XML files
Create an association between the XML file and the XML Schema. in Solution Explorer, double-click the CustomerList. xml file. The XML file is opened in the XML view of the designer. In the properties window, click the cell on the right of the targetSchema attribute and select http://tempuri.org/customerlistschema.xsd.pdf ".
Visual Studio adds a reference to the architecture in the CustomerList. xml file and adds Mark.
Add data to an XML file
9. now you can add data to the XML file. By associating the architecture with the XML file, the XML editor now knows the valid elements that can be included in the XML file and provides formatted grids in the data view.
Add data to the customerList. xml file. in the "customerList. XML" file in the xml view, position the cursor at the beginning and end Mark (start Mark = , End Mark = .
Type <. Select the Customer element.
Type> to end the tag.
Type <and select CompanyName from the valid element list.
Type> to end the tag.
Type Blue Yonder Airlines as the company name.
Switch to the "data" view. In the "contact name" field of the grid, type Nate Sun. Add data to other fields in the grid to fill in records. Switch back to the "XML" view. The data in the grid is now correctly formatted as XML.
II. create an XML schema from an XML file
Create a new XML architecture based on existing XML documents
1. load an XML document (. xml file) to the XML designer.
2. click "create schema" from the "XML" menu ".
3. an XML Schema (. xsd file) will be added to the current project, which has the same name as the original XML file.
4. load the newly created XML Schema (. xsd file) to the XML designer.
5. verify and edit the data types allocated when you create a schema.
Note: When deriving the schema from an existing XML document, all data types are set to string. Therefore, you must edit the data types according to the XML content requirements.
To change the architecture, you can use the XML designer to add, edit, and remove elements.
The above section describes how to create an XML file with an associated XML schema & create an XML schema from an XML file. For more information, see The PHP Chinese website (www.php1.cn )!