search
HomeBackend DevelopmentXML/RSS TutorialCreate an XML file with an associated XML schema && Create an XML schema from an XML file

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>

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


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>
   
      
         
         
         
         
         
      
   

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

 

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

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

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

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

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”文件中,将光标定位在开始和结束 标记(开始标记 = ,结束标记 = )之间。

键入

键入 > 以结束该标记。

键入

键入 > 以结束该标记。

键入 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中文网(www.php.cn)!


Statement
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
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools