XmlSchemaUnique Class

Represents the unique element from XML Schema as specified by the World Wide Web Consortium (W3C). 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)

public class XmlSchemaUnique : XmlSchemaIdentityConstraint
public class XmlSchemaUnique extends XmlSchemaIdentityConstraint
public class XmlSchemaUnique extends XmlSchemaIdentityConstraint

Specifies that an attribute or element value (or a combination of attribute or element values) must be unique within the specified scope. The constraint name must be unique within a schema.

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>

        // </xs:element>
        item.SchemaType = ct1;

        // </xs:sequence>
        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");


        // </xs:complexType>

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

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

        // </xs:element>

        XmlSchemaSet schemaSet = new XmlSchemaSet();
        schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne);

        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)

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:element name="item" minOccurs="0" maxOccurs="unbounded">
					<xs:attribute name="itemID" type="xs:string"/>
		<xs:attribute name="CustomerID" type="xs:string"/>

	<xs:element name="ordersByCustomer">
				<xs:element name="customerOrders" type="customerOrderType" minOccurs="0" maxOccurs="unbounded" />
		<xs:unique name="oneCustomerOrdersforEachCustomerID">
			<xs:selector xpath="customerOrders"/>
			<xs:field xpath="@customerID"/>

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

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0