Create an XML file with an associated XML schema && Create an XML schema from an XML file

黄舟
Release: 2017-02-28 16:48:23
Original
1941 people have browsed it

1. Create an XML file with associated xml schema
1. Create a new Windows application project

First you 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 representing 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}".

The pattern facet allows you to enter regular expressions. The regular expression \d{5} means that the postalCode type content is limited to 5 digits. 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 within a simple type.

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 sample does not include the actual declaration part of the framework, nor does it include what is called the root or document level Marked actual schema markup):

<xs:simpleType name="postalCode">
      <xs:restriction base="xs:positiveInteger">
         <xs:pattern value="\d{5}" />
      </xs:restriction>
   </xs:simpleType>
Copy after login

Select Save All from the File menu.

5. Add a complex type definition to the schema

Create a complex type element representing a standard US address

Switch to the "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 that cell because the element is the 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
Copy after login


Please note the assignment The data type given 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 tag called the root or document level Actual schema markup):

<xs:simpleType name="postalCode">
      <xs:restriction base="xs:positiveInteger">
         <xs:pattern value="\d{5}" />
      </xs:restriction>
   </xs:simpleType>
   
      
         
         
         
         
         
      
   
Copy after login

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
Copy after login


请注意分配给“帐单地址”(BillToAddress) 元素以及“发货地址”(ShipToAddress) 元素的数据类型。它是以前创建的 usAddress 复杂类型。我们可能已经为“电子邮件”(Email)、“电话号码”(Phone) 元素等定义了简单类型。

如果将架构切换到 XML 视图,应在根级别架构标记中看到下列代码(这意味着该代码示例既不包括框架的实际声明部分,也不包括称为根或文档级别标记的实际架构标记):

  <xs:simpleType name="postalCode">
      <xs:restriction base="xs:positiveInteger">
         <xs:pattern value="\d{5}" />
      </xs:restriction>
   </xs:simpleType>
   
      
         
         
         
         
         
      
   
   
      
         
            
            
            
            
            
            
         
      
   
Copy after login

从“文件”菜单中选择“全部保存”。

7、为了在 XML 文档内允许客户数据的多个实例,我们将创建名为 customerList 的元素,该元素将包含所有单独的 customer 元素。

创建 customerList 元素 从“工具箱”的“XML 架构”选项卡中将一个“element”拖到设计图面上。 选择默认名称“element1”,然后将其重命名为“customerList”。不要为此元素选择数据类型。 选择 customer 元素(以前创建的)并将其拖到 customerList 元素上。 单独的设计窗格进行绑定以表示数据的分层结构。 从“文件”菜单中选择“全部保存”。

8、将架构和 XML 文件相关联

创建 XML 文件和 XML 架构之间的关联 在“解决方案资源管理器”中,双击“CustomerList.xml”文件。该 XML 文件在设计器的 XML 视图中打开。 在“属性”窗口中,单击“targetSchema”属性右边的单元格,并选择“http://tempuri.org/CustomerListSchema.xsd”。

Visual Studio 在 CustomerList.xml 文件中添加对架构的引用,并添加 标记。

向 XML 文件添加数据

9、现在可以向 XML 文件添加数据了。通过将架构与 XML 文件关联,XML 编辑器现在知道可包括在 XML 文件中的有效元素,并在数据视图中提供格式化的网格。

向 customerList.xml 文件添加数据 ,在处于 XML 视图的“customerList.xml”文件中,将光标定位在开始和结束 标记(开始标记 = ,结束标记 = )之间。

键入 <。选择 Customer 元素。

键入 > 以结束该标记。

键入 <,并从有效元素列表中选择 CompanyName。

键入 > 以结束该标记。

键入 Blue Yonder Airlines 作为公司名。

切换到“数据”视图。 在网格中的“联系人姓名”字段中键入 Nate Sun。 通过在网格中的其他字段中添加数据来填写记录。 切回到“XML”视图。 网格中的数据现在正确格式化为 XML。

二、从 XML 文件创建 XML 架构

基于现有 XML 文档创建新的 XML 架构

1、将一个 XML 文档(.xml 文件)加载到“XML 设计器”中。

2、从“XML”菜单单击“创建架构”。

3、将向当前项目添加一个 XML 架构(.xsd 文件),它具有与原始 XML 文件相同的名称。

4、将新创建的 XML 架构(.xsd 文件)加载到“XML 设计器”中。

5、验证和编辑创建架构时分配的数据类型。

注意   当从现有 XML 文档推导架构时,所有数据类型开始都设置为 string,因此您必须根据 XML 数据的内容要求编辑数据类型。

如果需要对架构进行更改,可以使用“XML 设计器”添加、编辑和移除元素。

 以上就是创建带有关联的 XML 架构的 XML 文件 && 从 XML 文件创建 XML 架构的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!