XmlSchemaSimpleTypeUnion Class
.NET Framework 3.0
Represents the union element for simple types from XML Schema as specified by the . 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.
Namespace: System.Xml.Schema
Assembly: System.Xml (in system.xml.dll)
Assembly: System.Xml (in system.xml.dll)
The following example shows the use of XmlSchemaSimpleTypeUnion class.
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); } }
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>
System.Object
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaSimpleTypeContent
System.Xml.Schema.XmlSchemaSimpleTypeUnion
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaSimpleTypeContent
System.Xml.Schema.XmlSchemaSimpleTypeUnion
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show: