使用XML Schema

2017-08-05  本文已影响49人  Brent姜

从XML生成XSD

XML Schema Definition Tool (Xsd.exe)可以从XML文件生成XML Schema。

xsd.exe也可以用来从C# assembly生成XSD文件,How to create a XSD schema from a class?

All you have to do is compiling your assembly and run XSD.exe with the path to your assembly as argument. XSD.exe /? has a list of all arguments as well.

Example: XSD.exe C:\Dev\Project1\Bin\Debug\library.dll

从XSD生成C# Classes

很老的一个参考:Code Generation in the .NET Framework Using XML Schema, Daniel Cazzulino, May 2004
How to generate .NET 4.0 classes from xsd?

不过最简单的回答还是来自于How to generate .NET 4.0 classes from xsd?

simple enough; just run (at the vs command prompt)
xsd your.xsd /classes(which will create your.cs).
Note, however, that most of the intrinsic options here haven't changed much since 2.0
For the options, use xsd /?
or see MSDN; for example /enableDataBinding can be useful.

统一说明和Visual Studio集成工具

How to: Use the XML Schema Definition Tool to Generate Classes and XML Schema Documents

Visual Studio Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization.

往自动创建的Class属性添加自定义Attribute

Custom C# code generation from XSD中描述的一样,我要在create C# class library from XSD. The properties of generated C# file should be decorated with custom attribute。

参考SchemaImporterExtension Technology Sample, custom SchemaImporterExtension that allows fine control over code generation when an XML schema is imported. The application shows how to build, register and invoke this extension.+

同一个XSD生成多个XML

Controlling XML Serialization Using Attributes说:

Attributes can be used to control the XML serialization of an object or to create an alternate XML stream from the same set of classes. For more details about creating an alternate XML stream, see How to: Specify an Alternate Element Name for an XML Stream.+

类结构

Controlling XML Serialization Using Attributes讲了很多通过C# Attributes来改变从Class生成XML的效果,这篇文章非常重要。

从最基本的结构开始:

public class Group{  
    public Employee[] Employees;  
}  
public class Employee{  
    public string Name;  
}

可能生成的XML是:

<Group>  
    <Employees>  
        <Employee>  
            <Name>Haley</Name>  
        </Employee>  
    </Employees >  
</Group>  

使用Attributes可以改变XML 元素名称、实现继承类。

附录 DTD和Schema

XML documents can have a reference to a DTD or to an XML Schema.

A Simple XML Document

Look at this simple XML document called "note.xml":

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

A DTD File

The following example is a DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

The first line defines the note element to have four child elements: "to, from, heading, body".

Line 2-5 defines the to, from, heading, body elements to be of type "#PCDATA".

An XML Schema

An XML Schema describes the structure of an XML document.
The XML Schema language is also referred to as XML Schema Definition (XSD).
The following example is an XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"):

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

The note element is a complex type because it contains other elements. The other elements (to, from, heading, body) are simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.

上一篇 下一篇

猜你喜欢

热点阅读