Microsoft KB Archive/317688

= HOW TO: Create a Key Element for an XML Schema =

Article ID: 317688

Article Last Modified on 5/16/2007

-

APPLIES TO


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

-



This article was previously published under Q317688



IN THIS TASK
SUMMARY
 * Create an XML Schema
 * Create a Visual Basic .NET Application

REFERENCES



SUMMARY
Use this step-by-step guide to programmatically add a key element to an existing Extensible Markup Language (XML) schema.

With relational data representation, you can create a primary key or a unique key to identify a single instance of data or a row. A key element is necessary to achieve a similar, unique representation of data in hierarchical data. XML data representation is an example of hierarchical data representation. In an XML schema, a key element makes the data representation unique. The XmlSchemaKey class identifies a key constraint and represents the World Wide Web Consortium (W3C) key element.

The XmlSchema object contains the definition of a schema. All XML Schema Definition Language (XSD) elements are children of the schema element. XmlSchema represents the World Wide Web Consortium schema element.

back to the top

Create an XML Schema
In Microsoft Notepad or another text editor, type or paste the following code, and then save the file as Key.xsd.              </xs:complexType> </xs:element>

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

Create a Visual Basic .NET Application
<ol> <li>Follow these steps to create a new Windows application in Visual Basic .NET: <ol style="list-style-type: lower-alpha;"> <li>Start Microsoft Visual Studio .NET.</li> <li>On the File menu, point to New, and then click Project.</li> <li>In the New Project dialog box, click Visual Basic Projects under Project Types, click Windows Application under Templates, and then click OK.</li></ol> </li> <li>Drag a Button control from the toolbox to Form1.</li> <li> Import the following namespaces: Imports System.Xml Imports System.Xml.Schema Imports System.IO                   </li> <li> Add the following code as a Sub procedure that triggers Validation events for the XmlSchema object: Public Sub ValidationEventHandler(ByVal sender As Object, ByVal args As System.Xml.Schema.ValidationEventArgs) Console.WriteLine(args.Message) End Sub </li> <li> Add the following code in the Click event of Button1: Try Dim fstream As New FileStream(&quot;C:\key.xsd&quot;, FileMode.Open) Dim myschema As New XmlSchema

myschema = XmlSchema.Read(fstream, Nothing) myschema.Compile(AddressOf ValidationEventHandler)

Dim schemaobjecttable As XmlSchemaObjectTable Dim ParentElement As XmlSchemaElement Dim ParentElementQName As New XmlQualifiedName(&quot;Parent&quot;, &quot;&quot;)

schemaobjecttable = myschema.Elements If schemaobjecttable.Contains(ParentElementQName) Then ParentElement = CType(schemaobjecttable.Item(ParentElementQName), XmlSchemaElement)

Dim element_key As New XmlSchemaKey element_key.Name = &quot;IDKey&quot; element_key.Selector = New XmlSchemaXPath element_key.Selector.XPath = &quot;Children/Child&quot;

Dim field As New XmlSchemaXPath field.XPath = &quot;@name&quot; element_key.Fields.Add(field) ParentElement.Constraints.Add(element_key) End If

myschema.Compile(AddressOf ValidationEventHandler)

'Write to an external file. Dim strmWriter As New StreamWriter(&quot;c:\NewSchema.xsd&quot;) myschema.Write(strmWriter) strmWriter.Close 'Write to the Output window. Dim strWriter As New StringWriter myschema.Write(strWriter) Console.WriteLine(strWriter.ToString) Catch GenExp As Exception Console.WriteLine(GenExp.Message) Catch XMLExp As XmlException Console.WriteLine(XMLExp.Message) Catch XmlSchemaExp As XmlSchemaException Console.WriteLine(XmlSchemaExp.Message) End Try </li> <li> In the Output window, notice that 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> Additionally, the NewSchema.xsd file is created in the root folder (C:\). </li></ol>

back to the top

<div class="references_section">