.NET Framework 为 form 属性提供绑定支持。
Xsd.exe 工具将 form XML 属性 (Attribute) 等同于 XmlAttribute 或 XmlElement 属性 (Attribute) 的 Form 属性 (Property),尽管 .NET Framework 的 XML 序列化代码识别是另一个默认值 qualified
(对于元素而言)。
说明
XML 架构要求全局声明为 <schema> 元素子级的所有元素和属性在实例文档中都显示已限定了命名空间。 对于在 <complexType> 定义中局部声明的元素和属性,命名空间限定是可选的。
若要指定是否限定特定局部元素或属性的命名空间,请将 form 属性添加到局部的 <element> 或 <attribute> 声明中。 可能的值为 qualified
和 unqualified
。
form 属性的默认值是从根元素 <schema> 的 elementFormDefault 或 attributeFormDefault 属性继承的。 对于以上两个属性,默认值均为 unqualified
。
.NET Framework 无法在类定义内从全局级别或架构范围级别指定 form。 当基于一组类生成 XML 架构文档时,Xsd.exe 始终生成具有下列特征的 XML 架构:
不指定 attributeFormDefault,恢复为默认的
unqualified
。指定
elementFormDefault="qualified"
,实际使用与 XML 架构不同的默认值。
Xsd.exe 通过在每个字段的基础上设置 form,来响应 attributeFormDefault="qualified"
和 elementFormDefault="unqualified"
默认值的替代值。
在给定这些约束的基础上,Xsd.exe 按下表所示基于 form 属性生成源代码。
form 属性 |
form="qualified" |
form="unqualified" |
<attribute> |
向 XmlAttribute 属性声明传递 |
不向 <attribute> 字段的 XmlAttribute 属性声明传递 form 参数。 |
<element> |
不应用 XmlElement 属性,除非要传递不相关的参数。 |
向 <element> 字段的 XmlElement 属性声明传递参数 |
传递到 XmlAttribute 或 XmlElement 声明的参数设置 Form 属性,该属性的可能值来自 XmlSchemaForm 枚举:
XmlSchemaForm.Qualified
XmlSchemaForm.Unqualified
XmlSchemaForm.None:默认值
如果 Form 在原始 XML 架构文档中设置为非默认值,将只在源代码中根据 .NET Framework 默认值而非 XML 架构默认值指定 Form。 由于 form 属性重写 attributeFormDefault 和 elementFormDefault 属性,因此,如果一个架构在 <schema> 级别将 form 设置为非默认值,然后在 <attribute> 或 <element> 级别将其设置为默认值,则该架构将生成未指定 form 的源代码。 从符合实例文档的角度来讲,仍是如此。
示例
输入 XML 架构文档:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/" targetNamespace="http://example.org/" elementFormDefault="qualified">
<xsd:element name="ComplexInstance" type="MyComplexType"/>
<xsd:complexType name="MyComplexType">
<xsd:sequence>
<xsd:element name="elementQ" type="xsd:decimal" form="qualified" />
<xsd:element name="elementU" type="xsd:Date" form="unqualified" />
</xsd:sequence>
<xsd:attribute name="attributeQ" type="xsd:string" use="required" form="qualified"/>
<xsd:attribute name="attributeU" type="xsd:boolean" use="required" form="unqualified"/>
</xsd:complexType>
</xsd:schema>
基于前面的 XML 架构文档生成的 C# 类:
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("ComplexInstance", Namespace="http://example.org/", IsNullable=false)]
public class MyComplexType {
public System.Decimal elementQ;
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string elementU;
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string attributeQ;
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool attributeU;
}
基于通过前面的 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="ComplexInstance" type="tns:MyComplexType" />
<xs:complexType name="MyComplexType">
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="1" name="elementQ" type="xs:decimal" />
<xs:element minOccurs="0" maxOccurs="1" form="unqualified" name="elementU" type="xs:string" />
</xs:sequence>
<xs:attribute form="qualified" name="attributeQ" type="xs:string" />
<xs:attribute name="attributeU" type="xs:boolean" use="required" />
</xs:complexType>
</xs:schema>
可能的包含元素:<attribute>、<element>
请参见
参考
System.Xml.Schema.XmlSchemaAttribute.Form
System.Xml.Schema.XmlSchemaElement.Form
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。