XmlSchemaAttribute 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
按万维网联合会 (W3C) 指定的方式表示来自 XML 架构的 attribute 元素。 属性为其他文档元素提供额外信息。 属性标记嵌套在架构的文档元素标记之间。 XML 文档将属性显示为元素开始标记中的已命名项。
public ref class XmlSchemaAttribute : System::Xml::Schema::XmlSchemaAnnotatedpublic class XmlSchemaAttribute : System.Xml.Schema.XmlSchemaAnnotatedtype XmlSchemaAttribute = class
    inherit XmlSchemaAnnotatedPublic Class XmlSchemaAttribute
Inherits XmlSchemaAnnotated- 继承
示例
以下示例创建 attribute 元素。
using System;
using System.Xml;
using System.Xml.Schema;
class XMLSchemaExamples
{
    public static void Main()
    {
        XmlSchema schema = new XmlSchema();
        // <xs:attribute name="mybaseattribute">
        XmlSchemaAttribute attributeBase = new XmlSchemaAttribute();
        schema.Items.Add(attributeBase);
        attributeBase.Name = "mybaseattribute";
        // <xs:simpleType>
        XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType();
        attributeBase.SchemaType = simpleType;
        // <xs:restriction base="integer">
        XmlSchemaSimpleTypeRestriction restriction = new XmlSchemaSimpleTypeRestriction();
        simpleType.Content = restriction;
        restriction.BaseTypeName = new XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema");
        // <xs:maxInclusive value="1000"/>
        XmlSchemaMaxInclusiveFacet maxInclusive = new XmlSchemaMaxInclusiveFacet();
        restriction.Facets.Add(maxInclusive);
        maxInclusive.Value = "1000";
        // <xs:complexType name="myComplexType">
        XmlSchemaComplexType complexType = new XmlSchemaComplexType();
        schema.Items.Add(complexType);
        complexType.Name = "myComplexType";
        // <xs:attribute ref="mybaseattribute"/>
        XmlSchemaAttribute attributeBaseRef = new XmlSchemaAttribute();
        complexType.Attributes.Add(attributeBaseRef);
        attributeBaseRef.RefName = new XmlQualifiedName("mybaseattribute");
        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);
        schemaSet.Add(schema);
        schemaSet.Compile();
        XmlSchema compiledSchema = null;
        foreach (XmlSchema schema1 in schemaSet.Schemas())
        {
            compiledSchema = schema1;
        }
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema.Write(Console.Out, nsmgr);
    }
    public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
    {
        Console.WriteLine(args.Message);
    }
}
Option Explicit On
Option Strict On
Imports System.Xml
Imports System.Xml.Schema
Class XMLSchemaExamples
    Public Shared Sub Main()
        Dim schema As New XmlSchema()
        ' <xs:attribute name="mybaseattribute">
        Dim attributeBase As New XmlSchemaAttribute()
        schema.Items.Add(attributeBase)
        attributeBase.Name = "mybaseattribute"
        ' <xs:simpleType>
        Dim simpleType As New XmlSchemaSimpleType()
        attributeBase.SchemaType = simpleType
        ' <xs:restriction base="integer">
        Dim restriction As New XmlSchemaSimpleTypeRestriction()
        simpleType.Content = restriction
        restriction.BaseTypeName = New XmlQualifiedName("integer", "http://www.w3.org/2001/XMLSchema")
        ' <xs:maxInclusive value="1000"/>
        Dim maxInclusive As New XmlSchemaMaxInclusiveFacet()
        restriction.Facets.Add(maxInclusive)
        maxInclusive.Value = "1000"
        ' <xs:complexType name="myComplexType">
        Dim complexType As New XmlSchemaComplexType()
        schema.Items.Add(complexType)
        complexType.Name = "myComplexType"
        ' <xs:attribute ref="mybaseattribute"/>
        Dim attributeBaseRef As New XmlSchemaAttribute()
        complexType.Attributes.Add(attributeBaseRef)
        attributeBaseRef.RefName = New XmlQualifiedName("mybaseattribute")
        Dim schemaSet As New XmlSchemaSet()
        AddHandler schemaSet.ValidationEventHandler, AddressOf ValidationCallbackOne
        schemaSet.Add(schema)
        schemaSet.Compile()
        Dim compiledSchema As XmlSchema = Nothing
        For Each schema1 As XmlSchema In schemaSet.Schemas()
            compiledSchema = schema1
        Next
        Dim nsmgr As New XmlNamespaceManager(New NameTable())
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
        compiledSchema.Write(Console.Out, nsmgr)
    End Sub
    Public Shared Sub ValidationCallbackOne(ByVal sender As Object, ByVal args As ValidationEventArgs)
        Console.WriteLine(args.Message)
    End Sub
End Class
为前面的代码示例生成以下 XML 文件。
<?xml version="1.0" encoding="IBM437"?>
<xs:schema  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:attribute name="mybaseattribute">
        <xs:simpleType>
            <xs:restriction base="xs:integer">
                <xs:maxInclusive value="1000" />
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:complexType name="myComplexType">
        <xs:attribute ref="mybaseattribute" />
    </xs:complexType>
</xs:schema>
注解
属性声明可以作为元素的 schema 子元素呈现, (具有全局作用域) 或复杂类型定义中。 对于复杂类型,属性声明可作为本地声明或对具有全局范围的属性的引用出现。 全局和本地属性声明都具有引用现有简单类型的可选类型属性。 如果未使用可选类型属性,则属性声明 (全局或本地) 必须定义本地简单类型。
构造函数
| XmlSchemaAttribute() | 初始化 XmlSchemaAttribute 类的新实例。 | 
属性
| Annotation | 获取或设置  | 
| AttributeSchemaType | 获取一个 XmlSchemaSimpleType 对象,该对象表示基于特性的 SchemaType 或 SchemaTypeName 的特性类型。 | 
| AttributeType | 
				已过时.
			 
				已过时.
			 
				已过时.
			 基于保存编译后  | 
| DefaultValue | 获取或设置属性的默认值。 | 
| FixedValue | 获取或设置属性的固定值。 | 
| Form | 获取或设置属性的窗体。 | 
| Id | 获取或设置字符串 ID。(继承自 XmlSchemaAnnotated) | 
| LineNumber | 获取或设置  | 
| LinePosition | 获取或设置  | 
| Name | 获取或设置属性的名称。 | 
| Namespaces | 获取或设置用于此架构对象的 XmlSerializerNamespaces。(继承自 XmlSchemaObject) | 
| Parent | 获取或设置此 XmlSchemaObject 的父级。(继承自 XmlSchemaObject) | 
| QualifiedName | 获取属性的限定名称。 | 
| RefName | 获取或设置在此架构(或指定命名空间指示的另一个架构)中声明的属性的名称。 | 
| SchemaType | 获取属性类型或将其设置为简单类型。 | 
| SchemaTypeName | 获取或设置在此架构(或指定命名空间指示的另一个架构)中定义的简单类型的名称。 | 
| SourceUri | 获取或设置加载了架构的文件的源位置。(继承自 XmlSchemaObject) | 
| UnhandledAttributes | 获取或设置不属于当前架构目标命名空间的限定特性。(继承自 XmlSchemaAnnotated) | 
| Use | 获取或设置如何使用该属性的相关信息。 | 
方法
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) |