本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.
.NET Framework 为 <list> 元素提供部分绑定支持。
.NET Framework 为具有列表类型的 XML 特性声明提供准确的绑定,但不为元素声明提供准确的绑定。
说明
<list> 构造用于定义一个简单类型,该简单类型的可能值是用空白分隔的一系列值,这些值来自另一个简单类型。充当构造块的类型被称为原子类型,因为它们无法再划分为任何有意义的类型。除列表类型或联合类型以外的所有简单类型都是原子类型。
下面的示例演示如何创建值可以是用空白分隔的十进制数的列表类型:
<xsd:simpleType name="numbersList">
<xsd:list itemType="xsd:Decimal"/>
</xsd:simpleType>
当 .NET Framework 定义特性类型(而非元素类型)时,会为 <list> 元素提供绑定。
当从 XML 架构文档生成源代码时,如果 Xsd.exe 遇到列表类型的元素,则会生成一个字段,该字段的类型是与列表的构成类型(itemTypeXmlAttributeAttribute 特性的值)对应的 .NET Framework 类型。对于基于字符串的类型,由于字符串的字符串仍是字符串,因此在技术上此转换是正确的。对于基于其他简单类型的列表类型,例如上一示例中基于十进制的类型,该转换是错误的。
如果 Xsd.exe 遇到列表类型的特性,则会生成一个字段,该字段的类型是与 itemType 特性的值对应的 .NET Framework 类型的数组。该字段与特性一起出现。下面是生成的字段的一个示例:
[System.Xml.Serialization.XmlAttributeAttribute()]
public System.Decimal[] numbers;
同样,当基于一组类生成 XML 架构文档时,Xsd.exe 会在字段或属性中查找下列条件以生成列表类型:
该字段或属性的类型是可以映射到简单类型的类型的数组。
该字段或属性与 XmlAttribute 特性一起出现。
这一组合只能表示为 <list> 类型,因为 XML 特性必须具有简单类型,否则数组将绑定到复杂类型(maxOccurs 特性设置为 unbounded
)。
示例
输入 XML 架构文档:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
<xsd:element name="alphabet" type="AlphabetType"/>
<xsd:complexType name="AlphabetType">
<xsd:sequence>
<xsd:element name="language" type="xsd:string"/>
<xsd:element name="count" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="letters">
<xsd:simpleType>
<xsd:list itemType="Character"/>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="Character">
<xsd:restriction base="xsd:string">
<xsd:length value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
基于前面的 XML 架构文档生成的 C# 类:
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("alphabet", Namespace="http://example.org/", IsNullable=false)]
public class AlphabetType {
public string language;
public System.Decimal count;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string[] letters;
}
基于通过前面的 C# 源代码编译得到的程序集生成的 XML 架构复杂类型:
<xs:complexType name="AlphabetType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="language" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="count" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="letters">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
</xs:complexType>
可能的特性 | 绑定支持 |
---|---|
id |
Xsd.exe 实用工具会忽略旨在提供唯一标识符的 id 特性。 |
itemType |
当列表类型用作 XML 特性(而非元素)时,Xsd.exe 工具会生成一个字段,该字段是与 itemType 特性的值对应的 .NET Framework 类型的数组。 |
可能的父元素:<simpleType>
可能的子元素:<annotation>、<simpleType>