Microsoft KB Archive/318506

= HOW TO: Create Key Element for an XML Schema in Visual C# .NET =

Article ID: 318506

Article Last Modified on 9/4/2003

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q318506



For a Microsoft Visual Basic .NET version of this article, see 317688.

IN THIS TASK
SUMMARY
 * Requirements
 * Create an XML Schema
 * Create a Visual C# .NET Application

REFERENCES



SUMMARY
Use this step-by-step guide to programmatically add a key element to an existing XML schema.

In relational data representation, a primary or unique key is used to identify a single instance of data or a row. To get similar unique representation of data in hierarchical data, a key element is required. Data representation in Extensible Markup Language (XML) is an example of hierarchical data representation. In XML schema, a key element makes the data representation unique. XMLSchemaKey identifies a key constraint that represents the World Wide Web Consortium KEY element.

The XMLSchema object contains the definition of a schema. All XML schema definition language (XSD) elements are children of the schema element. The XMLSchema object represents the World Wide Web Consortium SCHEMA element.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET installed on a compatible Microsoft Windows operating system.

This article assumes that you are familiar with the following topics:
 * Microsoft Visual C# .NET language.
 * XML standards.
 * XSD schemas.

back to the top

Create an XML Schema
In Notepad or another text editor, type or paste the following code into a text file and name it C:\Key.xsd.              </xs:complexType> </xs:element>

</xs:sequence> </xs:complexType> </xs:element> </xs:schema> back to the top

Create a Visual C# .NET Application
<ol> <li>Create a new Visaul C# console application.</li> <li> Replace the code in the Class1.cs file with the following: using System; using System.Xml; using System.Xml.Schema; using System.IO;

namespace ConsoleApplication4 {   ///     /// Summary description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors); try {               FileStream fstream = new FileStream(&quot;C:\\key.xsd&quot;, FileMode.Open); XmlSchema myschema = new XmlSchema;

myschema = XmlSchema.Read(fstream, null); myschema.Compile(eventHandler);

XmlSchemaObjectTable schemaobjecttable; XmlSchemaElement ParentElement; XmlQualifiedName ParentElementQName = new XmlQualifiedName(&quot;Parent&quot;, &quot;&quot;);

schemaobjecttable = myschema.Elements; if (schemaobjecttable.Contains(ParentElementQName)) {                      // Set the parent Node. ParentElement = (XmlSchemaElement) schemaobjecttable[ParentElementQName]; // Create a new key element. XmlSchemaKey element_key = new XmlSchemaKey; element_key.Name = &quot;IDKey&quot;; element_key.Selector = new XmlSchemaXPath; element_key.Selector.XPath = &quot;Children/Child&quot;;

XmlSchemaXPath field = new XmlSchemaXPath; field.XPath = &quot;@name&quot;; element_key.Fields.Add(field); ParentElement.Constraints.Add(element_key); }

myschema.Compile(eventHandler);

// Write to an external file. StreamWriter strmWriter = new StreamWriter(&quot;c:\\NewSchema.xsd&quot;); myschema.Write(strmWriter); strmWriter.Close; // Write to Output window. StringWriter strWriter = new StringWriter; myschema.Write(strWriter); Console.WriteLine(strWriter.ToString); }           catch (XmlException XMLExp) {               Console.WriteLine(XMLExp.Message); }           catch (XmlSchemaException XmlSchemaExp) {               Console.WriteLine(XmlSchemaExp.Message); }           catch (Exception GenExp) {               Console.WriteLine(GenExp.Message); }           finally {               Console.Read; }       }        public static void ShowCompileErrors(object sender, ValidationEventArgs args) {           Console.WriteLine(&quot;Validation Error: {0}&quot;, args.Message); }   } }

XmlSchemaObjectTable: In the Output window, notice the XSD has a new element: <xs:key name=&quot;IDKey&quot;> <xs:selector xpath=&quot;Children/Child&quot; /> <xs:field xpath=&quot;@name&quot; /> </xs:key> A file named NewSchema.xsd is created in the root folder (C:\). </li></ol>

back to the top

<div class="references_section">