XmlSchemaUnique Class
.NET Framework 3.0
Represents the unique element from XML Schema as specified by the . This class can be used to identify a unique constraint among a set of elements.
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 the XmlSchemaUnique class.
using System; using System.Xml; using System.Xml.Schema; class XMLSchemaExamples { public static void Main() { XmlSchema schema = new XmlSchema(); // <xs:complexType name="customerOrderType"> XmlSchemaComplexType customerOrderType = new XmlSchemaComplexType(); customerOrderType.Name = "customerOrderType"; // <xs:sequence> XmlSchemaSequence sequence1 = new XmlSchemaSequence(); // <xs:element name="item" minOccurs="0" maxOccurs="unbounded"> XmlSchemaElement item = new XmlSchemaElement(); item.MinOccurs = 0; item.MaxOccursString = "unbounded"; item.Name = "item"; // <xs:complexType> XmlSchemaComplexType ct1 = new XmlSchemaComplexType(); // <xs:attribute name="itemID" type="xs:string"/> XmlSchemaAttribute itemID = new XmlSchemaAttribute(); itemID.Name = "itemID"; itemID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); // </xs:complexType> ct1.Attributes.Add(itemID); // </xs:element> item.SchemaType = ct1; // </xs:sequence> sequence1.Items.Add(item); customerOrderType.Particle = sequence1; // <xs:attribute name="CustomerID" type="xs:string"/> XmlSchemaAttribute CustomerID = new XmlSchemaAttribute(); CustomerID.Name = "CustomerID"; CustomerID.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); customerOrderType.Attributes.Add(CustomerID); // </xs:complexType> schema.Items.Add(customerOrderType); // <xs:element name="ordersByCustomer"> XmlSchemaElement ordersByCustomer = new XmlSchemaElement(); ordersByCustomer.Name = "ordersByCustomer"; // <xs:complexType> XmlSchemaComplexType ct2 = new XmlSchemaComplexType(); // <xs:sequence> XmlSchemaSequence sequence2 = new XmlSchemaSequence(); // <xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" /> XmlSchemaElement customerOrders = new XmlSchemaElement(); customerOrders.MinOccurs = 0; customerOrders.MaxOccursString = "unbounded"; customerOrders.Name = "customerOrders"; customerOrders.SchemaTypeName = new XmlQualifiedName("customerOrderType", ""); // </xs:sequence> sequence2.Items.Add(customerOrders); // </xs:complexType> ct2.Particle = sequence2; ordersByCustomer.SchemaType = ct2; // <xs:unique name="oneCustomerOrdersforEachCustomerID"> XmlSchemaUnique element_unique = new XmlSchemaUnique(); element_unique.Name = "oneCustomerOrdersforEachCustomerID"; // <xs:selector xpath="customerOrders"/> element_unique.Selector = new XmlSchemaXPath(); element_unique.Selector.XPath = "customerOrders"; // <xs:field xpath="@customerID"/> XmlSchemaXPath field = new XmlSchemaXPath(); field.XPath = "@customerID"; // </xs:unique> element_unique.Fields.Add(field); ordersByCustomer.Constraints.Add(element_unique); // </xs:element> schema.Items.Add(ordersByCustomer); 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:complexType name="customerOrderType"> <xs:sequence> <xs:element name="item" minOccurs="0" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="itemID" type="xs:string"/> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="CustomerID" type="xs:string"/> </xs:complexType> <xs:element name="ordersByCustomer"> <xs:complexType> <xs:sequence> <xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> <xs:unique name="oneCustomerOrdersforEachCustomerID"> <xs:selector xpath="customerOrders"/> <xs:field xpath="@customerID"/> </xs:unique> </xs:element> </xs:schema>
System.Object
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaIdentityConstraint
System.Xml.Schema.XmlSchemaUnique
System.Xml.Schema.XmlSchemaObject
System.Xml.Schema.XmlSchemaAnnotated
System.Xml.Schema.XmlSchemaIdentityConstraint
System.Xml.Schema.XmlSchemaUnique
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: