Editar

Partilhar via


XmlSchemaSimpleTypeUnion Class

Definition

Represents the union element for simple types from XML Schema as specified by the World Wide Web Consortium (W3C). A union datatype can be used to specify the content of a simpleType. The value of the simpleType element must be any one of a set of alternative datatypes specified in the union. Union types are always derived types and must comprise at least two alternative datatypes.

public ref class XmlSchemaSimpleTypeUnion : System::Xml::Schema::XmlSchemaSimpleTypeContent
public class XmlSchemaSimpleTypeUnion : System.Xml.Schema.XmlSchemaSimpleTypeContent
type XmlSchemaSimpleTypeUnion = class
    inherit XmlSchemaSimpleTypeContent
Public Class XmlSchemaSimpleTypeUnion
Inherits XmlSchemaSimpleTypeContent
Inheritance

Examples

The following example shows the use of XmlSchemaSimpleTypeUnion class.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

ref class XMLSchemaExamples
{
private:
    static void ValidationCallbackOne(Object^ sender, ValidationEventArgs^ args)
    {
        Console::WriteLine(args->Message);
    }
    
public:
    static void Main()
    {
        XmlSchema^ schema = gcnew XmlSchema();

        //<xs:simpleType name="StringOrIntType">
        XmlSchemaSimpleType^ StringOrIntType = gcnew XmlSchemaSimpleType();
        StringOrIntType->Name = "StringOrIntType";
        schema->Items->Add(StringOrIntType);

        // <xs:union>
        XmlSchemaSimpleTypeUnion^ union1 = gcnew XmlSchemaSimpleTypeUnion();
        StringOrIntType->Content = union1;

        // <xs:simpleType>
        XmlSchemaSimpleType^ simpleType1 = gcnew XmlSchemaSimpleType();
        union1->BaseTypes->Add(simpleType1);

        // <xs:restriction base="xs:string"/>
        XmlSchemaSimpleTypeRestriction^ restriction1 = gcnew XmlSchemaSimpleTypeRestriction();
        restriction1->BaseTypeName = gcnew XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        simpleType1->Content = restriction1;

        // <xs:simpleType>
        XmlSchemaSimpleType^ simpleType2 = gcnew XmlSchemaSimpleType();
        union1->BaseTypes->Add(simpleType2);

        // <xs:restriction base="xs:int"/>
        XmlSchemaSimpleTypeRestriction^ restriction2 = gcnew XmlSchemaSimpleTypeRestriction();
        restriction2->BaseTypeName = gcnew XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
        simpleType2->Content = restriction2;


        // <xs:element name="size" type="StringOrIntType"/>
        XmlSchemaElement^ elementSize = gcnew XmlSchemaElement();
        elementSize->Name = "size";
        elementSize->SchemaTypeName = gcnew XmlQualifiedName("StringOrIntType");
        schema->Items->Add(elementSize);

        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallbackOne);
        schemaSet->Add(schema);
        schemaSet->Compile();

        XmlSchema^ compiledSchema = nullptr;

        for each (XmlSchema^ schema1 in schemaSet->Schemas())
        {
            compiledSchema = schema1;
        }

        XmlNamespaceManager^ nsmgr = gcnew XmlNamespaceManager(gcnew NameTable());
        nsmgr->AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
        compiledSchema->Write(Console::Out, nsmgr);
    }
};

int main()
{
    XMLSchemaExamples::Main();
    return 0;
}
using System;
using System.Xml;
using System.Xml.Schema;

class XMLSchemaExamples
{
    public static void Main()
    {

        XmlSchema schema = new XmlSchema();

        //<xs:simpleType name="StringOrIntType">
        XmlSchemaSimpleType StringOrIntType = new XmlSchemaSimpleType();
        StringOrIntType.Name = "StringOrIntType";
        schema.Items.Add(StringOrIntType);

        // <xs:union>
        XmlSchemaSimpleTypeUnion union = new XmlSchemaSimpleTypeUnion();
        StringOrIntType.Content = union;

        // <xs:simpleType>
        XmlSchemaSimpleType simpleType1 = new XmlSchemaSimpleType();
        union.BaseTypes.Add(simpleType1);

        // <xs:restriction base="xs:string"/>
        XmlSchemaSimpleTypeRestriction restriction1 = new XmlSchemaSimpleTypeRestriction();
        restriction1.BaseTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
        simpleType1.Content = restriction1;

        // <xs:simpleType>
        XmlSchemaSimpleType simpleType2 = new XmlSchemaSimpleType();
        union.BaseTypes.Add(simpleType2);

        // <xs:restriction base="xs:int"/>
        XmlSchemaSimpleTypeRestriction restriction2 = new XmlSchemaSimpleTypeRestriction();
        restriction2.BaseTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");
        simpleType2.Content = restriction2;

        // <xs:element name="size" type="StringOrIntType"/>
        XmlSchemaElement elementSize = new XmlSchemaElement();
        elementSize.Name = "size";
        elementSize.SchemaTypeName = new XmlQualifiedName("StringOrIntType");
        schema.Items.Add(elementSize);

        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);
    }
}
Imports System.Xml
Imports System.Xml.Schema

Class XMLSchemaExamples
    Public Shared Sub Main()
        Dim schema As New XmlSchema()

        ' <xs:simpleType name="StringOrIntType">
        Dim StringOrIntType As New XmlSchemaSimpleType()
        StringOrIntType.Name = "StringOrIntType"
        schema.Items.Add(StringOrIntType)

        ' <xs:union>
        Dim union As New XmlSchemaSimpleTypeUnion
        StringOrIntType.Content = union

        ' <xs:simpleType>
        Dim simpleType1 As New XmlSchemaSimpleType
        union.BaseTypes.Add(simpleType1)

        ' <xs:restriction base="xs:string"/>
        Dim restriction1 As New XmlSchemaSimpleTypeRestriction()
        restriction1.BaseTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
        simpleType1.Content = restriction1

        ' <xs:simpleType>
        Dim simpleType2 As New XmlSchemaSimpleType()
        union.BaseTypes.Add(simpleType2)

        ' <xs:restriction base="xs:int"/>
        Dim restriction2 As New XmlSchemaSimpleTypeRestriction()
        restriction2.BaseTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
        simpleType2.Content = restriction2


        ' <xs:element name="size" type="StringOrIntType"/>
        Dim elementSize As New XmlSchemaElement()
        elementSize.Name = "size"
        elementSize.SchemaTypeName = New XmlQualifiedName("StringOrIntType")
        schema.Items.Add(elementSize)

        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

The following XML file is generated for this code example.

<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="StringOrIntType">
        <xs:union>
            <xs:simpleType>
                <xs:restriction base="xs:string"/>
            </xs:simpleType>
    
            <xs:simpleType>
                <xs:restriction base="xs:int"/>
            </xs:simpleType>
        </xs:union>
    </xs:simpleType>
    
    <xs:element name="size" type="StringOrIntType"/>
</xs:schema>

Remarks

A union type enables an element or attribute value to be one or more instances of one type drawn from the union of multiple atomic and list types.

Constructors

XmlSchemaSimpleTypeUnion()

Initializes a new instance of the XmlSchemaSimpleTypeUnion class.

Properties

Annotation

Gets or sets the annotation property.

(Inherited from XmlSchemaAnnotated)
BaseMemberTypes

Gets an array of XmlSchemaSimpleType objects representing the type of the simpleType element based on the BaseTypes and MemberTypes values of the simple type.

BaseTypes

Gets the collection of base types.

Id

Gets or sets the string id.

(Inherited from XmlSchemaAnnotated)
LineNumber

Gets or sets the line number in the file to which the schema element refers.

(Inherited from XmlSchemaObject)
LinePosition

Gets or sets the line position in the file to which the schema element refers.

(Inherited from XmlSchemaObject)
MemberTypes

Gets or sets the array of qualified member names of built-in data types or simpleType elements defined in this schema (or another schema indicated by the specified namespace).

Namespaces

Gets or sets the XmlSerializerNamespaces to use with this schema object.

(Inherited from XmlSchemaObject)
Parent

Gets or sets the parent of this XmlSchemaObject.

(Inherited from XmlSchemaObject)
SourceUri

Gets or sets the source location for the file that loaded the schema.

(Inherited from XmlSchemaObject)
UnhandledAttributes

Gets or sets the qualified attributes that do not belong to the current schema's target namespace.

(Inherited from XmlSchemaAnnotated)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to