public class XmlSchemaUnique : XmlSchemaIdentityConstraint
|
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);
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);
}
}
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>
| ctor #1 | Default constructor. This constructor is called by derived class constructors to initialize state in this type. |
| Annotation (inherited from System.Xml.Schema.XmlSchemaAnnotated) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaAnnotated.Annotation Gets or sets the annotation property. |
| Fields (inherited from System.Xml.Schema.XmlSchemaIdentityConstraint) |
Read-only See base class member description: System.Xml.Schema.XmlSchemaIdentityConstraint.Fields Gets the collection of fields that apply as children for the XML Path Language (XPath) expression selector. |
| Id (inherited from System.Xml.Schema.XmlSchemaAnnotated) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaAnnotated.Id Gets or sets the string id. |
| LineNumber (inherited from System.Xml.Schema.XmlSchemaObject) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaObject.LineNumber Gets or sets the line number in the file to which the schema element refers. |
| LinePosition (inherited from System.Xml.Schema.XmlSchemaObject) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaObject.LinePosition Gets or sets the line position in the file to which the schema element refers. |
| Name (inherited from System.Xml.Schema.XmlSchemaIdentityConstraint) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaIdentityConstraint.Name Gets or sets the name of the identity constraint. |
| Namespaces (inherited from System.Xml.Schema.XmlSchemaObject) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaObject.Namespaces |
| QualifiedName (inherited from System.Xml.Schema.XmlSchemaIdentityConstraint) |
Read-only See base class member description: System.Xml.Schema.XmlSchemaIdentityConstraint.QualifiedName Gets the qualified name of the identity constraint which holds the post-compilation value of the QualifiedName property. |
| Selector (inherited from System.Xml.Schema.XmlSchemaIdentityConstraint) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaIdentityConstraint.Selector Gets or sets the XML Path Language (XPath) expression selector element. |
| SourceUri (inherited from System.Xml.Schema.XmlSchemaObject) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaObject.SourceUri Gets or sets the source location for the file that loaded the schema. |
| UnhandledAttributes (inherited from System.Xml.Schema.XmlSchemaAnnotated) |
Read-write See base class member description: System.Xml.Schema.XmlSchemaAnnotated.UnhandledAttributes Gets or sets the qualified attributes that do not belong to the schema target namespace. |
| Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
| GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
| GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
| ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
| Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
| MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public XmlSchemaUnique(); |
public XmlSchemaAnnotation Annotation {get; set;}
|
public XmlSchemaObjectCollection Fields {get;}
|
public string Id {get; set;}
|
Optional.
public int LineNumber {get; set;}
|
public int LinePosition {get; set;}
|
public string Name {get; set;}
|
public XmlSerializerNamespaces Namespaces {get; set;}
|
public XmlQualifiedName QualifiedName {get;}
|
public XmlSchemaXPath Selector {get; set;}
|
public string SourceUri {get; set;}
|
public XmlAttribute[] UnhandledAttributes {get; set;}
|
~XmlSchemaUnique(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |