Export (0) Print
Expand All

XmlSchemaKeyref Class

This class represents the keyref element from XMLSchema as specified by the .

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

public class XmlSchemaKeyref : XmlSchemaIdentityConstraint
public class XmlSchemaKeyref extends XmlSchemaIdentityConstraint
public class XmlSchemaKeyref extends XmlSchemaIdentityConstraint
Not applicable.

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

import System.*;
import System.Collections.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Xsl.*;
import System.Xml.Schema.*;

public class XmlSchemaObjectGenerator
 {        
    private static void ValidationCallback(Object sender,
        ValidationEventArgs args)
    {
        if (args.get_Severity().Equals(XmlSeverityType.Warning)) {
            Console.Write("WARNING: ");
        }
        else {        
            if (args.get_Severity().Equals(XmlSeverityType.Error)) {
                Console.Write("ERROR: ");
            }
        } 
        Console.WriteLine(args.get_Message());
    } //ValidationCallback

    public static void main(String[] args)
    {
        XmlTextReader tr = new XmlTextReader("empty.xsd");
        XmlSchema schema = XmlSchema.Read
            (tr, new ValidationEventHandler(ValidationCallback));
        schema.set_ElementFormDefault(XmlSchemaForm.Qualified);
        schema.set_TargetNamespace("http://www.example.com/Report");
        {
            XmlSchemaElement element = new XmlSchemaElement();
            element.set_Name("purchaseReport");
            XmlSchemaComplexType elementComplexType =  
                new XmlSchemaComplexType();
            XmlSchemaSequence elementComplexTypeSequence = 
                new XmlSchemaSequence();
            {
                XmlSchemaElement elementComplexTypeSequenceElement = 
                    new XmlSchemaElement();
                elementComplexTypeSequenceElement.set_Name("region");
                elementComplexTypeSequenceElement.set_SchemaTypeName
                    (new XmlQualifiedName("string", 
                    "http://www.w3.org/2001/XMLSchema"));
                {
                    XmlSchemaKeyref 
                        elementComplexTypeSequenceElementKeyref = 
                        new XmlSchemaKeyref();
                    elementComplexTypeSequenceElementKeyref.set_Name
                        ("dummy2");
                    elementComplexTypeSequenceElementKeyref.set_Selector
                        (new XmlSchemaXPath());
                    elementComplexTypeSequenceElementKeyref.get_Selector()
                        .set_XPath("r:zip/r:part");
                    {
                        XmlSchemaXPath field = new XmlSchemaXPath();
                        field.set_XPath("@number");
                        elementComplexTypeSequenceElementKeyref
                            .get_Fields().Add(field);
                    }
                    elementComplexTypeSequenceElementKeyref.set_Refer
                        (new XmlQualifiedName("pNumKey", 
                        "http://www.example.com/Report"));
                    elementComplexTypeSequenceElement.get_Constraints()
                        .Add(elementComplexTypeSequenceElementKeyref);
                }
                elementComplexTypeSequence.get_Items().Add
                    (elementComplexTypeSequenceElement);
            }
            elementComplexType.set_Particle(elementComplexTypeSequence);
            {
                XmlSchemaAttribute elementComplexTypeAttribute = 
                    new XmlSchemaAttribute();
                elementComplexTypeAttribute.set_Name("periodEnding");
                elementComplexTypeAttribute.set_SchemaTypeName
                    (new XmlQualifiedName("date", 
                    "http://www.w3.org/2001/XMLSchema"));
                elementComplexType.get_Attributes().Add
                    (elementComplexTypeAttribute);
            }
            element.set_SchemaType(elementComplexType);
            {
                XmlSchemaKey elementKey = new XmlSchemaKey();
                elementKey.set_Name("pNumKey");
                elementKey.set_Selector(new XmlSchemaXPath());
                elementKey.get_Selector().set_XPath("r:parts/r:part");
                {
                    XmlSchemaXPath field = new XmlSchemaXPath();
                    field.set_XPath("@number");
                    elementKey.get_Fields().Add(field);
                }
                element.get_Constraints().Add(elementKey);
            }
            schema.get_Items().Add(element);
        }
        schema.Write(Console.get_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>

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

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft