本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.
.NET Framework 为 name 特性提供绑定支持。
name 特性的值与公共类或字段的名称对应,具体情况取决于要应用该特性的元素。可以应用适当的 ***Name 属性,以便重写在 Xsd.exe 生成 XSD 文档时生成的名称。
从 XSD 文档生成源代码
当从 XML 架构文档生成源代码时,name 特性用于 Xsd.exe 为其提供类型绑定的元素,如下所述:
<attribute>:提供表示该特性的公共类字段的名称。该字段与 System.Xml.Serialization.XmlAttributeAttribute 一起出现。
<complexType>:提供与复杂类型相对应的类的名称。
<element>:提供表示该元素的公共类字段的名称。如果 <element> 元素包含匿名的 <complexType> 定义,则该名称会变成与复杂类型相对应的类的名称。
<simpleType>:提供与简单类型相对应的类的名称。.NET Framework 只允许通过以下方法来派生简单类型:(a) 枚举基于字符串的类型;(b) 创建用作特性的列表。
不要为了遵循编码约定而试图更改大小写。例如,如果 <complexType> 元素的 name 特性的值为 testInfo
,则结果类的名称为 testInfo,而不是首字母大写的 TestInfo。如果名称与保留的关键字冲突,则会生成一个带有符号 @ 前缀的名称。
.NET Framework 不绑定到下列用于建立唯一性和引用完整性的 XML 架构定义语言元素:<key>、<keyref> 和 <unique>。因此,将忽略在这些元素中出现的 name 特性。同样,对于 <notation> 元素也没有任何类型绑定,因此将忽略该元素中的 name 特性。
由于 Xsd.exe 不直接支持 <group> 和 <attributeGroup> 元素,而是将每个引用的内容扩展为与包含引用的每个复杂类型相对应的类,因此将忽略该组或特性组名。
基于类生成 XSD 文档
当 Xsd.exe 基于程序集中的一组类生成 XSD 文档时,会使用以前描述的代码构造,为对应的 name 特性提供值。
另外,除了字段以外,还将公共属性转换为 <element> 和 <attribute> 元素,因此,默认情况下会将属性名用作 name 特性值。
可通过下列特性属性来提供替代名称 - name 特性值:
System.Xml.Serialization.XmlAttributeAttribute.AttributeName
System.Xml.Serialization.XmlTypeAttribute.TypeName:更改与已经应用了该特性的 .NET Framework 类型相对应的 XSD 数据类型的名称。
System.Xml.Serialization.XmlArrayAttribute.ElementName:更改数组的父 XML 元素的名称。
System.Xml.Serialization.XmlArrayItemAttribute.ElementName:更改与数组项相对应的连续子元素的名称。
可能的包含元素:<attribute>、<attributeGroup>、<complexType>、<element>、<group>、<key>、<keyref>、<notation>、<simpleType>、<unique>
另请参见
参考
System.Xml.Schema.XmlSchemaAttribute.Name
System.Xml.Schema.XmlSchemaAttributeGroup.Name
System.Xml.Schema.XmlSchemaElement.Name
System.Xml.Schema.XmlSchemaGroup.Name
System.Xml.Schema.XmlSchemaType.Name
XmlSchemaComplexType
XmlSchemaSimpleType
System.Xml.Schema.XmlSchemaIdentityConstraint.Name
XmlSchemaKey
XmlSchemaKeyref
XmlSchemaUnique
System.Xml.Schema.XmlSchemaNotation.Name