Export (0) Print
Expand All

XmlSchemaAttribute Class

Attributes provide additional information for other document elements. The attribute tag is nested inbetween the tags of a documents element for the schema. The XML document displays attributes as named item in the opening tag of an element. Represents the World Wide Web Consortium (W3C) attribute element.

For a list of all members of this type, see XmlSchemaAttribute Members.

System.Object
   System.Xml.Schema.XmlSchemaObject
      System.Xml.Schema.XmlSchemaAnnotated
         System.Xml.Schema.XmlSchemaAttribute

[Visual Basic]
Public Class XmlSchemaAttribute
   Inherits XmlSchemaAnnotated
[C#]
public class XmlSchemaAttribute : XmlSchemaAnnotated
[C++]
public __gc class XmlSchemaAttribute : public XmlSchemaAnnotated
[JScript]
public class XmlSchemaAttribute extends XmlSchemaAnnotated

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Attribute declarations can be present as child elements of the schema element (having global scope) or within complex type definitions. For complex types, attribute declarations can be present as local declarations or references to attributes with global scope. Both global and local attribute declarations have the optional type attribute that refers to an existing simple type. If the optional type attribute is not used, the attribute declaration (global or local) must define a local simple type.

Example

[Visual Basic, C#, C++] The following example creates the attribute element.

[Visual Basic] 
Option Explicit
Option Strict

Imports System
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")
        
        schema.Compile(AddressOf ValidationCallbackOne)
        Dim nsmgr As New XmlNamespaceManager(New NameTable())
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
        schema.Write(Console.Out, nsmgr)
    End Sub 'Main
    
    
    Public Shared Sub ValidationCallbackOne(sender As Object, args As ValidationEventArgs)

        Console.WriteLine(args.Message)
    End Sub 'ValidationCallbackOne
End Class 'XMLSchemaExamples

[C#] 
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");
        
        schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
     XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        schema.Write(Console.Out, nsmgr);
    }

    public static void ValidationCallbackOne(object sender, ValidationEventArgs args) {

        Console.WriteLine(args.Message);
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;  
using namespace System::Xml::Schema;

__gc class XMLSchemaExamples {
public:
    static void main() {
 
        XmlSchema* schema = new XmlSchema();

        // <xs:attribute name="mybaseattribute">
        XmlSchemaAttribute* attributeBase = new XmlSchemaAttribute();
        schema->Items->Add(attributeBase);
        attributeBase->Name = S"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(S"integer", S"http://www.w3.org/2001/XMLSchema");
        
        // <xs:maxInclusive value="1000"/>
        XmlSchemaMaxInclusiveFacet* maxInclusive = new XmlSchemaMaxInclusiveFacet();
        restriction->Facets->Add(maxInclusive);
        maxInclusive->Value = S"1000";

        // <xs:complexType name="myComplexType">
        XmlSchemaComplexType* complexType = new XmlSchemaComplexType();
        schema->Items->Add(complexType);
        complexType->Name = S"myComplexType";
        
        // <xs:attribute ref="mybaseattribute"/>
        XmlSchemaAttribute* attributeBaseRef = new XmlSchemaAttribute();
        complexType->Attributes->Add(attributeBaseRef);
        attributeBaseRef->RefName = new XmlQualifiedName(S"mybaseattribute");
        
        schema->Compile(new ValidationEventHandler(0, ValidationCallbackOne));
     XmlNamespaceManager* nsmgr = new XmlNamespaceManager(new NameTable());
        nsmgr->AddNamespace(S"xs", S"http://www.w3.org/2001/XMLSchema");
        schema->Write(Console::Out,nsmgr);
    }

    static void ValidationCallbackOne(Object* /*sender*/, ValidationEventArgs* args) {

        Console::WriteLine(args->Message);
    }
};

int main()
{
    XMLSchemaExamples::main();
}

[Visual Basic, C#, C++] The following XML file is generated for the preceding code example.

<?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>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml.Schema

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Xml (in System.Xml.dll)

See Also

XmlSchemaAttribute Members | System.Xml.Schema Namespace

Show:
© 2014 Microsoft