本主题专门介绍一项旧有技术。现在应通过使用以下链接来创建 XML Web 服务和 XML Web 服务客户端: Windows Communication Foundation.
.NET Framework 为 fixed 特性提供绑定支持。
说明
fixed 特性可以出现在 <element> 或 <attribute> 声明中,以确定元素或特性在规范的 XML 文档中所必须具有的常数值。该特性还可以与除 <enumeration> 和 <pattern> 以外的其他任何限制方面元素一起出现。在这种情况下,值 true
可防止派生更改伴随的方面值。
.NET Framework 并未并入数据类型绑定或序列化的限制方面(基于字符串的枚举例外),因此会忽略 fixed 特性及其所在的方面。架构对象模型提供了一个具有 IsFixed 属性的 XmlSchemaFacet 基类。
如果 fixed 特性出现在 <element> 或 <attribute> 元素中,Xsd.exe 会将该字段静态初始化为默认值,如下面的示例所示:
public int age = -1;
按照 XML 架构,fixed 特性的值必须是 XML 架构简单类型。有关 Xsd.exe 如何转换简单类型的固定/默认值的详细信息,请参见 default 特性。
对于元素和特性,架构对象模型使用 XmlSchemaAttribute 和 XmlSchemaElement 类的 FixedValue 属性来表示 fixed 特性。
Example
输入 XML 架构文档:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
<xsd:element name="FamilyDog" type="FamilyDogType"/>
<xsd:complexType name="FamilyDogType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string" fixed="Spot"/>
<xsd:element name="birthdate" type="xsd:date" />
</xsd:sequence>
<xsd:attribute name="gender" type="GenderType" fixed="UNKNOWN"/>
<xsd:attribute name="fixed" type="xsd:boolean" fixed="false"/>
<xsd:attribute name="breed" type="xsd:string" fixed="Swedish Vallhund"/>
</xsd:complexType>
<xsd:simpleType name="GenderType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="FEMALE" />
<xsd:enumeration value="MALE" />
<xsd:enumeration value="UNKNOWN" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
基于前面的 XML 架构文档生成的 C# 类:
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("FamilyDog", Namespace="http://example.org/", IsNullable=false)]
public class FamilyDogType {
public string name = "Spot";
[System.Xml.Serialization.XmlElementAttribute(DataType="date")]
public System.DateTime birthdate;
[System.Xml.Serialization.XmlAttributeAttribute()]
public GenderType gender = GenderType.UNKNOWN;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool genderSpecified;
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool @fixed = false;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool fixedSpecified;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string breed = "Swedish Vallhund";
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public enum GenderType {
FEMALE,
MALE,
UNKNOWN,
}
基于通过前面的 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="FamilyDog" type="tns:FamilyDogType" />
<xs:complexType name="FamilyDogType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="name" type="xs:string" fixed="Spot"/>
<xs:element minOccurs="1" maxOccurs="1" name="birthdate" type="xs:date" />
</xs:sequence>
<xs:attribute name="gender" type="tns:GenderType" />
<xs:attribute name="fixed" type="xs:boolean" />
<xs:attribute name="breed" type="xs:string" />
</xs:complexType>
<xs:simpleType name="GenderType">
<xs:restriction base="xs:string">
<xs:enumeration value="FEMALE" />
<xs:enumeration value="MALE" />
<xs:enumeration value="UNKNOWN" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
可能的包含元素:<attribute>、<element> 以及各种限制方面
另请参见
参考
System.Xml.Schema.XmlSchemaAttribute.FixedValue
System.Xml.Schema.XmlSchemaElement.FixedValue
System.Xml.Schema.XmlSchemaFacet.IsFixed