element 元素绑定支持

本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.

.NET Framework 为 <element> 元素提供绑定支持。

说明

XML 架构规范规定,可以在复杂类型定义内局部声明某个元素,也可以通过全局声明的方式将它声明为根元素 <schema> 的子级。如果该元素是全局声明的,则可以通过 ref 特性从一个或多个复杂类型定义引用它。

下面的代码示例演示一个局部声明的元素。

<xsd:complexType name="UnitType">
  <xsd:sequence>
   <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>

下面的代码示例演示先进行全局声明然后引用的同一元素。

<xsd:element name="name" type="xsd:string"/>
<xsd:complexType name="UnitType">
  <xsd:sequence>
    <xsd:element ref="name"/>
  </xsd:sequence>
</xsd:complexType>

当从 XML 架构复杂类型生成 .NET Framework 类型时,除非全局元素是在该架构的目标命名空间之外的命名空间中声明的,否则 Xsd.exe 不区分局部声明的元素和对全局声明的元素的引用。

同一命名空间内的引用

由于 Xsd.exe 不在同一命名空间内对二者进行区分,因此,当执行 XML 架构与类之间的往返转换时,会创建一些局部元素,这些元素将取代对全局元素的任何引用。

如果所引用的全局元素的数据类型已自动绑定到 .NET Framework 类型,则 Xsd.exe 将不会生成与该数据类型对应的新类型。因此,在往返行程中生成的 XML 架构中,不但不再引用全局元素声明,而且将不再显示该声明。

awac9czf.note(zh-cn,VS.100).gif注意:
所有内置的 XML 架构数据类型都自动绑定到 .NET Framework 类型。此外,架构定义的简单类型绑定到 .NET Framework 类型,而不是绑定到自定义类型,但有一个例外。这个例外涉及到字符串绑定类型的枚举;在关于 <enumeration> 元素的文档中,有对此例外的更详细说明。

awac9czf.note(zh-cn,VS.100).gif注意:
有关对架构定义的简单类型的支持的更多信息,请参见 <restriction> 元素或表示限制 Facet 的任何元素。

对另一个命名空间的引用

如果所引用的全局声明属于另一个命名空间,则 Xsd.exe 使用应用于所生成的字段的 XmlElementAttribute 特性的 Namespace 属性来指定该命名空间。对于该特定元素,使用 Namespace 属性指定的命名空间会重写使用 XmlTypeAttribute 特性和 XmlRootAttribute(可选)在类级别指定的命名空间,如下面的示例所示。

[System.Xml.Serialization.XmlElementAttribute(Namespace="http://example.org/elem")]
public string Text;

其他命名空间是使用 <import> 元素导入到 XML 架构定义中的。

创建全局元素声明

当基于程序集中的一组类生成 XML 架构文档时,Xsd.exe 会为它基于该程序集中定义的类型生成的每个 <complexType><simpleType> 定义生成一个全局 <element> 声明。

第一个示例显示了当定义全局元素的目标命名空间中包含对该元素的引用时,Xsd.exe 如何处理该全局元素。

输入 XML 架构文档。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="Text" type="xsd:normalizedString"/>
  <xsd:complexType name="Branch">
    <xsd:sequence>
      <xsd:element name="children" type="xsd:token" minOccurs="0" maxOccurs="unbounded" />
      <xsd:element ref="Text" />
    </xsd:sequence>
    <xsd:attribute name="key" type="xsd:token"/>
  </xsd:complexType>
  <xsd:element name="branch" type="Branch"/>
</xsd:schema>

基于前面的 XML 架构文档生成的 C# 类。

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("branch", Namespace="http://example.org/", IsNullable=false)]
public class Branch {
        
    [System.Xml.Serialization.XmlElementAttribute("children", DataType="token")]
    public string[] children;
        
    [System.Xml.Serialization.XmlElementAttribute(DataType="normalizedString")]
    public string Text;
        
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string key;
}

基于通过前面的 C# 源代码编译得到的程序集生成的 XML 架构文档。

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="branch" type="tns:Branch" />
  <xs:complexType name="Branch">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="children" type="xs:token" />
      <xs:element minOccurs="0" maxOccurs="1" name="Text" type="xs:normalizedString" />
    </xs:sequence>
    <xs:attribute name="key" type="xs:token" />
  </xs:complexType>
</xs:schema>

在前面生成的 XML 架构中,Text 元素(最初是全局声明的)已成为局部元素。

第二个示例演示当在一个单独的命名空间中定义某个全局元素时,Xsd.exe 如何处理对该元素的引用。此示例使用 <import> 元素导入另一个位于单独的 XSD 文件中的命名空间。(<import> 元素的 schemaLocation 特性不用于指定导入的 .xsd 文件的位置。对于 Xsd.exe 而言,该文件作为一个附加的命令行参数来指定。)

用作输入的顶级 XML 架构文档。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
      xmlns="http://example.org/" targetNamespace="http://example.org/" xmlns:elem="http://example.org/elem">
  <xsd:import  namespace="http://example.org/elem" />
  <xsd:complexType name="Branch">
    <xsd:sequence>
      <xsd:element name="children" type="xsd:token" minOccurs="0" maxOccurs="unbounded" />
      <xsd:element ref="elem:Text" />
    </xsd:sequence>
    <xsd:attribute name="key" type="xsd:token"/>
  </xsd:complexType>
  <xsd:element name="branch" type="Branch"/>
</xsd:schema>

用作输入的已导入 XML 架构文档。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" 
      xmlns="http://example.org/elem" targetNamespace="http://example.org/elem">
  <xsd:element name="Text" type="xsd:normalizedString"/>
</xsd:schema>

基于前面的两个 XML 架构文档生成的 C# 类。

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("branch", Namespace="http://example.org/", IsNullable=false)]
public class Branch {
        
    [System.Xml.Serialization.XmlElementAttribute("children", DataType="token")]
    public string[] children;
        
    [System.Xml.Serialization.XmlElementAttribute(Namespace="http://example.org/elem", DataType="normalizedString")]
    public string Text;
        
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string key;
}

基于通过前面的 C# 源代码编译得到的程序集生成的顶级 XML 架构文档。

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://example.org/elem" />
  <xs:element name="branch" type="tns:Branch" />
  <xs:complexType name="Branch">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="unbounded" name="children" type="xs:token" />
      <xs:element minOccurs="0" maxOccurs="1" xmlns:q1="http://example.org/elem" ref="q1:Text" />
    </xs:sequence>
    <xs:attribute name="key" type="xs:token" />
  </xs:complexType>
</xs:schema>

基于通过前面的 C# 源代码编译得到的程序集生成的已导入 XML 架构文档。

<xs:schema xmlns:tns="http://example.org/elem" elementFormDefault="qualified" targetNamespace="http://example.org/elem" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Text" type="xs:normalizedString" />
</xs:schema>

abstract 特性

全局元素声明可以具有 abstract 特性;而局部元素则不能。

如果将某个元素声明为抽象元素 (abstract="true"),则会强制使用替换组替换它;如果该元素不是抽象的,则替换是可选的。

被替换的全局元素称作头元素。只要局部元素声明使用 ref 特性引用头元素,您就可以用任何全局替换元素来替换该局部元素。有关更多信息,请参见 substitutionGroup 特性。

如果替换元素属于派生的数据类型,而且头元素本身被声明为抽象(而不是元素类型的 <complexType> 定义被声明为抽象),则尽管您不能在发生替换的位置使用基类型,但您可以在 XML 实例文档中的其他位置使用它。

当从 XML 架构文档生成源代码时,Xsd.exe 生成一个与头元素的数据类型对应的类型。该类型包括一个名为 Item 的字段。对于每个替换元素,Xsd.exe 都向该字段应用一个 XmlElementAttribute。每个特性都标识名称,而且,如果替换元素具有派生的数据类型,还标识该类型。如果替换元素使用与头相同的数据类型,则还必须应用 XmlChoiceIdentifierAttribute。有关更多信息,请参见 <choice> 元素。

下面的代码示例演示一个示例字段 Item,该字段是从对抽象头元素的引用生成的。

[System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
public MyBaseType Item;

如果要使用属于派生数据类型的替换元素,那么,只有在向该字段分配了某个派生类型的实例时,XML 序列化功能才有效。如果已经分配了与头元素对应的基类型的实例,XmlSerializer 类将失败。

在进行反向转换时,Xsd.exe 会生成一个 <choice> 元素,其中包含与每个替换元素一一对应的 <element> 元素,但不包含表示头元素的 <element>。这些元素是在复杂类型定义内部单独定义的;不采用任何 ref 引用。

awac9czf.note(zh-cn,VS.100).gif注意:
在包含指定抽象头元素的 ref 特性的局部 <element> 声明中,Xsd.exe 会忽略 maxOccurs 特性。

awac9czf.note(zh-cn,VS.100).gif注意:
考虑下面对一个 MyBaseType 类型的头元素的声明。

<xsd:element ref="baseInstance" minOccurs="0" maxOccurs="unbounded" />
awac9czf.note(zh-cn,VS.100).gif注意:
对于前面的声明,Xsd.exe 仍生成以下字段(省略了特性)。

public MyBaseType Item;
awac9czf.note(zh-cn,VS.100).gif注意:
如果输入 XML 架构中所有全局声明的元素均为抽象元素,则 Xsd.exe 将无法生成类。

示例:abstract 特性

输入 XML 架构文档。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="baseInstance" type="MyBaseType" abstract="true"/>

  <xsd:complexType name="MyBaseType">
    <xsd:sequence>
      <xsd:element name="Field1" type="xsd:string"/>
      <xsd:element name="Field2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="derivedAInstance" type="DerivedTypeA" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeA">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForA" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="derivedBInstance" type="DerivedTypeB" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeB">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForB" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="myThings" type="MyThingsType" />
  <xsd:complexType name="MyThingsType">
    <xsd:sequence>
      <xsd:element ref="baseInstance" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

基于前面的 XML 架构文档生成的 C# 类。

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedAInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeA : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForA;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeB))]
public class MyBaseType {
    public string Field1;
        
    public string Field2;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedBInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeB : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForB;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("myThings", Namespace="http://example.org/", IsNullable=false)]
public class MyThingsType {       
    [System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
    [System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
    public MyBaseType Item;
}

基于通过前面的 C# 源代码编译得到的程序集生成的 XML 架构文档。

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyBaseType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Field1" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="DerivedTypeA">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForA" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="DerivedTypeB">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForB" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="derivedAInstance" type="tns:DerivedTypeA" />
  <xs:element name="derivedBInstance" type="tns:DerivedTypeB" />
  <xs:element name="myThings" type="tns:MyThingsType" />
  <xs:complexType name="MyThingsType">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" name="derivedAInstance" type="tns:DerivedTypeA" />
        <xs:element minOccurs="0" maxOccurs="1" name="derivedBInstance" type="tns:DerivedTypeB" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:schema> 

substitutionGroup 特性

仅当被替换元素声明为抽象元素时,Xsd.exe 工具才能识别替换组。(请参见前面的 Abstract Attribute一节。)然后,在进行反向转换时,Xsd.exe 会生成一个 <choice> 元素,对于每个替换元素,其中都包含有一个 <element> 元素。

substitutionGroup 特性:背景

substitutionGroup 特性可以出现于全局声明的元素中,使该元素可以取代 XML 实例文档中另一个全局声明的元素。特性值是要替换的元素(称为头元素)的名称。实际的替换可以发生在复杂类型定义内的 <element> 元素中,在该定义中,头元素是使用 ref 特性引用的。用 substitutionGroup 特性声明的任何元素都必须与头元素属于相同的数据类型,或者属于头元素数据类型的派生类型。

使用替换组,可以允许在实例文档中的指定位置出现不同名称和(基类型相同的)不同类型的元素(可选)。

不支持嵌套替换组。即,如果一个元素在一个替换组中作为头元素,它就不能在另一个替换组中成为被替换元素。例如,

<xs:element name="Animal" abstract="true"/>
<xs:element name="Mammal" abstract="true" 
    substitutionGroup="tns:Animal"/>
<xs:element name="Dog" type="xs:string" 
    substitutionGroup="tns:Mammal"/>

不支持该操作,因为 Mammal 既作为头元素 (Dog) 也作为被替换元素 (Animal)。如果遇到此类情形,Xsd.exe 不会为 Mammal 生成 XmlElementAttribute,Dog 也不会在 Animal 元素应该出现的地方。但是,您可以进行手动添加以使其起作用,如下面的示例所示。

public class PetShop 
{
    private object Animal;
    
    // Apply the XmlElementAttribute to the generated property.
    [System.Xml.Serialization.XmlElementAttribute("Mammal"), 
     System.Xml.Serialization.XmlElementAttribute("Dog")]
    public object Animal {
        get {
            return this.Animal;
        }
        set {
            this.Animal = value;
        }
    }
}

substitutionGroup 特性:具体的头元素

如果头元素不是抽象元素,则 Xsd.exe 会提取对头元素的每个 ref 引用,创建一个类型与头元素的 XML 数据类型对应的字段或数组,具体取决于引用元素的 maxOccurs 特性的值。对替换组的任何引用都将丢失。

尽管可以向该字段分配派生类型的对象,而且 XmlSerializer 类会序列化该对象,但是,该行为发生在使用派生类型的替换之外。不使用替换元素名称。

substitutionGroup 特性:抽象头元素

如果头元素是抽象元素,则 Xsd.exe 会生成一个类型与头元素的数据类型对应的字段。该字段接收名称 Item。对于每个替换元素,Xsd.exe 都向该字段应用一个 XmlElementAttribute。每个特性都标识名称,而且,如果替换元素具有派生的数据类型,还标识该类型。如果替换元素使用与头相同的数据类型,则还必须应用 XmlChoiceIdentifierAttribute。请参见 <choice> 元素。

下面的示例字段是基于对抽象头元素的引用生成的。

[System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
public MyBaseType Item;

如果使用派生类型,那么,只有在向该字段分配了某个派生类型的实例时,XML 序列化功能才有效。如果已经分配了与头元素对应的基类型的实例,XmlSerializer 将失败。

在进行反向转换时,Xsd.exe 会从此构造生成一个 <choice> 元素,其中包含与每个替换元素(而非头元素)一一对应的 <element> 元素。这些元素是在复杂类型定义内部单独定义的;不采用任何 ref 引用。

maxOccurs attribute. 对于包含指定抽象头元素的 ref 特性的局部 <element> 声明,Xsd.exe 忽略 maxOccurs 特性。

请考虑下面的声明,它引用一个 MyBaseType 类型的头元素。

<xsd:element ref="baseInstance" minOccurs="0" maxOccurs="unbounded" />

对于前面的声明,Xsd.exe 仍生成以下字段(省略了特性)。

public MyBaseType Item;

示例:substitutionGroup 特性

输入 XML 架构文档。

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
  <xsd:element name="baseInstance" type="MyBaseType" abstract="true"/>

  <xsd:complexType name="MyBaseType">
    <xsd:sequence>
      <xsd:element name="Field1" type="xsd:string"/>
      <xsd:element name="Field2" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:element name="derivedAInstance" type="DerivedTypeA" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeA">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForA" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="derivedBInstance" type="DerivedTypeB" substitutionGroup="baseInstance" />
  <xsd:complexType name="DerivedTypeB">
    <xsd:complexContent>
      <xsd:extension base="MyBaseType">
         <xsd:attribute name="ExtraInfoForB" type="xsd:string"/>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>

  <xsd:element name="myThings" type="MyThingsType" />
  <xsd:complexType name="MyThingsType">
    <xsd:sequence>
      <xsd:element ref="baseInstance" />
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>

基于前面的 XML 架构文档生成的 C# 类。

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedAInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeA : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForA;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeA))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedTypeB))]
public class MyBaseType {
    public string Field1;
        
    public string Field2;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("derivedBInstance", Namespace="http://example.org/", IsNullable=false)]
public class DerivedTypeB : MyBaseType {
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string ExtraInfoForB;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("myThings", Namespace="http://example.org/", IsNullable=false)]
public class MyThingsType {       
    [System.Xml.Serialization.XmlElementAttribute("derivedAInstance", typeof(DerivedTypeA))]
    [System.Xml.Serialization.XmlElementAttribute("derivedBInstance", typeof(DerivedTypeB))]
    public MyBaseType Item;
}

基于通过前面的 C# 源代码编译得到的程序集生成的 XML 架构文档。

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="MyBaseType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="Field1" type="xs:string" />
      <xs:element minOccurs="0" maxOccurs="1" name="Field2" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="DerivedTypeA">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForA" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="DerivedTypeB">
    <xs:complexContent mixed="false">
      <xs:extension base="tns:MyBaseType">
        <xs:attribute name="ExtraInfoForB" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="derivedAInstance" type="tns:DerivedTypeA" />
  <xs:element name="derivedBInstance" type="tns:DerivedTypeB" />
  <xs:element name="myThings" type="tns:MyThingsType" />
  <xs:complexType name="MyThingsType">
    <xs:sequence>
      <xs:choice minOccurs="1" maxOccurs="1">
        <xs:element minOccurs="0" maxOccurs="1" name="derivedAInstance" type="tns:DerivedTypeA" />
        <xs:element minOccurs="0" maxOccurs="1" name="derivedBInstance" type="tns:DerivedTypeB" />
      </xs:choice>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

可能的特性 绑定支持

abstract

Xsd.exe 实用工具使用抽象 <element> 声明绑定替换组,但在反向转换中生成 <choice> 元素。

请参见前面的“abstract 特性”一节。

block

block 特性可用来防止将元素声明用作替换组的头(组成员替换的元素)。

Xsd.exe 工具忽略 block 特性,以及 schema 元素绑定支持 元素的 blockDefault 特性。

default

default 特性提供默认值,如果该元素在实例文档中为空,将使用该默认值。如果该元素根本不出现,则不会填充它。

当从 XML 架构文档生成源代码时,Xsd.exe 工具会提取与具有默认值的元素对应的每个字段,应用 System.ComponentModel.DefaultValueAttribute,以将默认值作为参数传递。此外,Xsd.exe 还以静态方式将该字段初始化为默认值,如下面的示例所示。

[System.ComponentModel.DefaultValueAttribute(-1)]
public int age = -1;

在从具有 .NET Framework 值类型的类成员生成 <element> 元素时,Xsd.exe 将 DefaultValue 特性用作设置 minOccurs 特性时的输入。如果 Xsd.exe 遇到应用于此类成员的 DefaultValue 特性,会为该元素的 minOccurs 特性生成值 0。这表示该元素不必出现在有效的 XML 实例文档中。

在数组(maxOccurs 大于 1 的元素)上忽略默认特性。

请参见 default 特性绑定支持 特性。

final

final 特性可用来防止将元素声明用作替换组的头(组成员替换的元素)。

Xsd.exe 忽略 final 特性,以及 <schema> 元素的 finalDefault 特性。

fixed

fixed 特性为实例文档中的元素提供固定值。从 XML 架构文档生成源代码时,Xsd.exe 会提取与具有固定值的元素对应的每个字段,然后生成该字段的静态初始值设定项,如下面的示例所示。

public int age = -1;

在数组(maxOccurs 大于 1 的元素)上忽略 fixed 特性。

form

Xsd.exe 工具将 <element> 元素的 form XML 特性等同于 XmlElementAttributeForm 属性。.NET Framework 的 XML 序列化基础结构可识别另一默认值 qualified

如果 XML 架构中的 <element> 声明指定 form="unqualified",则 Xsd.exe 会为对应的字段生成 XmlElement 特性,并为该特性传递参数 Form=XmlSchemaForm.Unqualified

请参见 form 特性。

id

Xsd.exe 实用工具会忽略旨在提供唯一标识符的 id 特性。不过,Xsd.exe 会识别 name 特性。

maxOccurs

当从 XSD 文档生成类时,Xsd.exe 会按照下列可能值解释 <element> 元素中的 maxOccurs 特性:

  • 1:Xsd.exe 生成类型与元素的数据类型对应的字段。

  • 0:Xsd.exe 处理值 0 失败,而改为将该值作为默认值 1 处理。

  • unbounded:Xsd.exe 生成一个字段,该字段是类型与元素的数据类型对应的数组。

  • 大于 1 的任何整数:与 unbounded 值相同,Xsd.exe 生成一个字段,该字段是类型与元素的数据类型对应的数组。可以使用 XmlValidatingReader 类对照 SOM 表示的 XML 架构文档验证 XML 文档,从而强制使用大于 1 的值。

当基于程序集中的一组类生成 XML 架构文档时,Xsd.exe 执行上述转换的反向转换,从单个实例生成 maxOccurs1,从数组生成 maxOccursunbounded

尽管 Xsd.exe 将 maxOccursunbounded 绑定到数组,但是它将 maxOccurs1 绑定到数组的指定父元素(如果有的话)。有关针对数组绑定处理 maxOccurs 元素(包括指定额外的父元素)的更多信息,请参见 maxOccurs 特性绑定支持 特性。

minOccurs

对于 <element> 元素,只有当 maxOccurs 特性绑定支持 特性的值不指示数组字段时,Xsd.exe 才检查 minOccurs 特性的值。然后,从对应的字段属于引用类型还是值类型开始,按照各种因素解释(或生成)值。

请参见 minOccurs 特性绑定支持 特性。

name

如果要从 XSD 文档生成源代码,则 name 特性的值会提供表示该元素的公共类字段的名称。如果 <element> 元素包含匿名的 <complexType> 定义,则该名称会变成与复杂类型相对应的类的名称。

不要为了遵循编码约定而试图更改大小写。例如,如果包含匿名复杂类型定义的 <element> 的 name 特性具有值 testInfo,则结果类的名称将为 testInfo,而不是首字母大写的 TestInfo。如果名称与保留的关键字冲突,则会生成一个带有符号 @ 后缀的名称。

当 Xsd.exe 从某个公共类字段生成 <element> 声明时,会将该字段的名称用于 name 特性的值。可使用下列特性属性来提供替代名称 - name 特性值:

请参见 name 特性绑定支持 特性。

nillable

Xsd.exe 工具将 nillable 特性等同于某些应用于引用类型的 XML 序列化相关特性的 IsNullable 属性。对于值类型,值为 truenillable 导致生成可以为 null 的类型。

请参见 nillable 特性。nillable 特性仅出现在 <element> 元素中。

ref

当从 XML 架构复杂类型生成 .NET Framework 类型时,除非全局元素是在该架构的目标命名空间之外的命名空间中声明的,否则 Xsd.exe 不区分局部声明的元素和对全局声明的元素的引用。

请参见“同一命名空间内的引用”和“对另一个命名空间的引用”部分。

substitutionGroup

仅当被替换元素声明为抽象元素时,Xsd.exe 工具才能识别替换组。然后,在进行反向转换时,Xsd.exe 会生成一个 <choice> 元素,对于每个替换元素,其中都包含有一个 <element> 元素。

请参见前面的“substitutionGroup 特性”一节。

type

Xsd.exe tool 工具将使用 <element> 和 <attribute> 声明的 type 特性引用的数据类型与 .NET Framework 类型关联。

除非某个 XML 架构数据类型可以追溯到使用 type 特性引用某个数据类型(可能是另一个数据类型)的全局元素声明,否则 Xsd.exe 不会为该 XML 架构数据类型生成 .NET Framework 类型。

可能的父元素: <all><choice><schema><sequence>

可能的子元素: <annotation><complexType><key><keyref><simpleType><unique>

另请参见

参考

XmlSchemaElement