complexType 元素绑定支持

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

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

Xsd.exe 工具将 XML 架构复杂类型等同于公共字段或属性表示复杂类型的内容的 .NET Framework 类型。

说明

Xsd.exe 工具将 XML 架构复杂类型等同于公共字段表示复杂类型的内容的 .NET Framework 类型。

abstract 特性

复杂类型被声明为 abstract (abstract="true") 可以确保只有派生类型的实例出现在符合的 XML 文档中,而抽象基类型的实例不出现在该文档中。

Xsd.exe 将抽象复杂类型等同于抽象类。转换是在代码与 XSD 文档之间双向进行的。

除使用 abstract 关键字的情况外,在其他情况下,抽象类被当作非抽象基类处理。子类扩展基类。Xsd.exe 会对抽象类的每个子代类都应用一个 System.Xml.Serialization.XmlIncludeAttribute。每一个 XmlInclude 特性都指定子代类的类型。

示例:abstract 特性

下面的代码示例演示如何对 <complexType> 元素使用 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="FamilyNode" type="FamilyNodeType" />
  <xsd:complexType name="FamilyNodeType">
    <xsd:sequence>
      <xsd:element name="Code" type="xsd:string" />
      <xsd:element name="Parent" type="Parent" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="Daughter">
    <xsd:complexContent>
      <xsd:extension base="Parent">
        <xsd:sequence>
          <xsd:element name="Date" type="xsd:dateTime" minOccurs="0"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="Son">
    <xsd:complexContent>
      <xsd:extension base="Parent">
        <xsd:sequence>
          <xsd:element name="Info" type="xsd:string" minOccurs="0"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="Parent" abstract="true">
    <xsd:sequence>
      <xsd:element name="Text" type="xsd:normalizedString" />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="Grandchild">
    <xsd:complexContent>
      <xsd:extension base="Daughter">
        <xsd:attribute name="Parent" type="xsd:normalizedString" />
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

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

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("FamilyNode", Namespace="http://example.org/", IsNullable=false)]
public class FamilyNodeType {
        
    public string Code;
        
    public Parent Parent;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Son))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Daughter))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Grandchild))]
public abstract class Parent {
    [System.Xml.Serialization.XmlElementAttribute(DataType="normalizedString")]
    public string Text;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public class Son : Parent {        
    public string Info;
}
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Grandchild))]
public class Daughter : Parent {
        
    public System.DateTime Date;
        
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool DateSpecified;
}
    
 [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public class Grandchild : Daughter {        
    [System.Xml.Serialization.XmlAttributeAttribute("Parent", DataType="normalizedString")]
    public string Parent1;
}

基于通过从上面的 C# 源代码编译得到的类生成的 XML 架构实际上等同于原始 XML 架构。

可能的特性 绑定支持

abstract

Xsd.exe 实用工具将 abstract="true" 标识的抽象复杂类型等同于抽象类。对于从抽象类继承的每一个类,Xsd.exe 都会对该抽象类应用一个指定子代类类型的 System.Xml.Serialization.XmlIncludeAttribute

请参见前面的“Abstract Attribute”一节。

block

block 特性可以应用于数据类型,以防止派生类型在指定了原始类型的位置取代该原始类型。

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

final

final 特性可以应用于数据类型以防止将它用作基类型派生其他类型。

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

id

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

mixed

请参见 mixed 特性。

name

name 特性的值成为 Xsd.exe 从复杂类型生成的 .NET Framework 类型的名称。

不要为了遵循编码约定而试图更改大小写。例如,如果 <complexType> 元素的 name 特性的值为 testInfo,则结果类的名称为 testInfo,而不是首字母大写的 TestInfo。如果名称与保留的关键字冲突,则会生成一个带有符号 @ 前缀的名称。

当 Xsd.exe 基于类生成 <complexType> 定义时,它会将类名用作 name 特性的值。通过 TypeName 属性可以提供替代名称 - name 特性值。

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

可能的父元素:<element><redefine><schema>

可能的子元素:<all><annotation><anyAttribute><attribute><attributeGroup><choice><complexContent><group><sequence><simpleContent>

另请参见

参考

XmlSchemaComplexType