Export (0) Print
Expand All

XmlSchemaKeyref Class

This class represents the keyref element from XMLSchema as specified by the World Wide Web Consortium (W3C).

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public class XmlSchemaKeyref : XmlSchemaIdentityConstraint

The XmlSchemaKeyref type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkXmlSchemaKeyrefInitializes a new instance of the XmlSchemaKeyref class.
Top

  NameDescription
Public propertySupported by the XNA FrameworkAnnotationGets or sets the annotation property. (Inherited from XmlSchemaAnnotated.)
Public propertySupported by the XNA FrameworkFieldsGets the collection of fields that apply as children for the XML Path Language (XPath) expression selector. (Inherited from XmlSchemaIdentityConstraint.)
Public propertySupported by the XNA FrameworkIdGets or sets the string id. (Inherited from XmlSchemaAnnotated.)
Public propertySupported by the XNA FrameworkLineNumberGets or sets the line number in the file to which the schema element refers. (Inherited from XmlSchemaObject.)
Public propertySupported by the XNA FrameworkLinePositionGets or sets the line position in the file to which the schema element refers. (Inherited from XmlSchemaObject.)
Public propertySupported by the XNA FrameworkNameGets or sets the name of the identity constraint. (Inherited from XmlSchemaIdentityConstraint.)
Public propertySupported by the XNA FrameworkNamespacesGets or sets the XmlSerializerNamespaces to use with this schema object. (Inherited from XmlSchemaObject.)
Public propertySupported by the XNA FrameworkParentGets or sets the parent of this XmlSchemaObject. (Inherited from XmlSchemaObject.)
Public propertySupported by the XNA FrameworkQualifiedNameGets the qualified name of the identity constraint, which holds the post-compilation value of the QualifiedName property. (Inherited from XmlSchemaIdentityConstraint.)
Public propertySupported by the XNA FrameworkReferGets or sets the name of the key that this constraint refers to in another simple or complex type.
Public propertySupported by the XNA FrameworkSelectorGets or sets the XPath expression selector element. (Inherited from XmlSchemaIdentityConstraint.)
Public propertySupported by the XNA FrameworkSourceUriGets or sets the source location for the file that loaded the schema. (Inherited from XmlSchemaObject.)
Public propertySupported by the XNA FrameworkUnhandledAttributesGets or sets the qualified attributes that do not belong to the current schema's target namespace. (Inherited from XmlSchemaAnnotated.)
Top

  NameDescription
Public methodSupported by the XNA FrameworkEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Specifies that an attribute or element value (or set of values) has a correspondence with those of the specified key or unique element. The constraint name must be unique within a schema.

The following example creates a key element.

using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Schema;

public class XmlSchemaObjectGenerator {



    private static void ValidationCallback(object sender, ValidationEventArgs args ){

	if(args.Severity == XmlSeverityType.Warning)
	    Console.Write("WARNING: ");
	else if(args.Severity == XmlSeverityType.Error)
	    Console.Write("ERROR: ");

	Console.WriteLine(args.Message);
    }
	                  

    public static void Main() {

		 
	  XmlTextReader tr = new XmlTextReader("empty.xsd");
        XmlSchema schema = XmlSchema.Read(tr, new ValidationEventHandler(ValidationCallback));

            schema.ElementFormDefault = XmlSchemaForm.Qualified;

            schema.TargetNamespace = "http://www.example.com/Report";

            {

        XmlSchemaElement element = new XmlSchemaElement();
        element.Name = "purchaseReport";

        XmlSchemaComplexType element_complexType = new XmlSchemaComplexType();

        XmlSchemaSequence element_complexType_sequence = new XmlSchemaSequence();

            {

        XmlSchemaElement element_complexType_sequence_element = new XmlSchemaElement();
        element_complexType_sequence_element.Name = "region";
        element_complexType_sequence_element.SchemaTypeName = 
				new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
			;

            {

        XmlSchemaKeyref element_complexType_sequence_element_keyref = new XmlSchemaKeyref();
        element_complexType_sequence_element_keyref.Name = "dummy2";
        element_complexType_sequence_element_keyref.Selector = new XmlSchemaXPath();
			element_complexType_sequence_element_keyref.Selector.XPath = "r:zip/r:part";

            {
				XmlSchemaXPath field = new XmlSchemaXPath();
				
				field.XPath = "@number";
                element_complexType_sequence_element_keyref.Fields.Add(field);
            }
        element_complexType_sequence_element_keyref.Refer =  
				new XmlQualifiedName("pNumKey", "http://www.example.com/Report")
			;
        element_complexType_sequence_element.Constraints.Add(element_complexType_sequence_element_keyref);
            }
        element_complexType_sequence.Items.Add(element_complexType_sequence_element);
            }
        element_complexType.Particle = element_complexType_sequence;

            {

        XmlSchemaAttribute element_complexType_attribute = new XmlSchemaAttribute();
        element_complexType_attribute.Name = "periodEnding";
        element_complexType_attribute.SchemaTypeName = 
				new XmlQualifiedName("date", "http://www.w3.org/2001/XMLSchema")
			;
        element_complexType.Attributes.Add(element_complexType_attribute);
            }
        element.SchemaType = element_complexType;

            {

        XmlSchemaKey element_key = new XmlSchemaKey();
        element_key.Name = "pNumKey";
        element_key.Selector = new XmlSchemaXPath();
			element_key.Selector.XPath = "r:parts/r:part";

            {
				XmlSchemaXPath field = new XmlSchemaXPath();
				
				field.XPath = "@number";
                element_key.Fields.Add(field);
            }
        element.Constraints.Add(element_key);
            }

                schema.Items.Add(element);
            }

	    schema.Write(Console.Out);



    }/* Main() */ 

} 
//XmlSchemaObjectGenerator

The following XML file is generated for the preceding code example.

<?xml version="1.0" encoding="IBM437"?>
<xs:schema xmlns:r="http://www.example.com/Report" elementFormDefault="qualified" targetNamespace="http://www.example.com/Report" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="purchaseReport">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="region" type="xs:string">
          <xs:keyref name="dummy2" refer="r:pNumKey">
            <xs:selector xpath="r:zip/r:part" />
            <xs:field xpath="@number" />
          </xs:keyref>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="periodEnding" type="xs:date" />
    </xs:complexType>
    <xs:key name="pNumKey">
      <xs:selector xpath="r:parts/r:part" />
      <xs:field xpath="@number" />
    </xs:key>
  </xs:element>
</xs:schema>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

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