Microsoft KB Archive/816171

= How to create a key element for an XML schema in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 816171

Article Last Modified on 5/16/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0

-





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



For a Microsoft Visual C# .NET version of this article, see 318506.

This article references the following .NET Framework Class Library namespaces:
 * System::Xml
 * System::Xml::Schema
 * System::IO

IN THIS TASK
 SUMMARY  Create an XML Schema Create a Visual C++ .NET or Visual C++ 2005 Application

REFERENCES



SUMMARY
This step-by-step article describes how to programmatically add a key element to an existing XML schema.

You can create a primary key or a unique key to identify a single instance of the data or to identify a row with relational data representation. You must have a key element to gain a similar unique representation of the 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. The XmlSchema object represents the W3C schema element.

back to the top

Create an XML Schema
In Notepad or a text editor of your choice, paste the following code:   <xs:complexType> <xs:sequence> <xs:element name=&quot;Children&quot;> <xs:complexType> <xs:sequence> <xs:element name=&quot;Child&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;> <xs:complexType> <xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot; use=&quot;required&quot;/> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> Save the file as C:\Key.xsd.

back to the top

Create a Visual C++ .NET or Visual C++ 2005 Application
<ol> Create a new Visual C++ .NET or Visual C++ 2005 application. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.</li> On the File menu, point to New, and then click Project.</li> In Visual Studio .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual Studio 2005, click Visual C++ under Project Types, and then click CLR Console Application under Templates.</li> Type XMLKey in the Name text box, and then click OK.</li></ol> </li>  Open the XMLKey.cpp file, and then add the following lines of code below the #include directives: using namespace System; using namespace System::Xml; using namespace System::Xml::Schema; using namespace System::IO; </li> Add a reference to System.dll and to System.XML.dll. To do this, follow these steps:

Microsoft Visual Studio .NET 2002:   Add the following code before the using statement.
 * 1) using <System.dll>
 * 2) using <System.XML.dll> </li></ul>

Microsoft Visual Studio .NET 2003: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click References under XMLKey, and then click Add Reference.</li> On the .NET tab of the Add Reference dialog box, press and hold the CTRL key, click System.dll, click System.Xml.dll , and then click Select.</li> When both components appear under Selected Components, click OK.</li></ol>

Microsoft Visual Studio 2005: <ol style="list-style-type: lower-alpha;"> In Solution Explorer, right-click XMLKey, click Reference, and then click Add New Reference in the XMLKey Property Page dialog box.</li> On the .NET tab of the Add Reference dialog box, press and hold the CTRL key, click System.dll, click System.Xml.dll, and then click OK twice.</li></ol>

The components now appear under References in Solution Explorer.</li>  Add the following code before the int _tmain function. This code defines a __gc class that is named MyClass. MyClass has a static method that is named MyValidationEventHandler. This function is a call back function that triggers Validation events for the XmlSchema object. public __gc class MyClass { public: static void MyValidationEventHandler(Object* obj,ValidationEventArgs *args) {   Console::WriteLine(args->Message); } }; </li>  Add the following code in the int _tmain function: try {   ValidationEventHandler *veh = new ValidationEventHandler(0,&MyClass::MyValidationEventHandler); FileStream *fstream = new FileStream(S&quot;C:\\key.xsd&quot;, FileMode::Open); XmlSchema *myschema = new XmlSchema;

myschema = XmlSchema::Read(fstream,NULL); myschema->Compile(veh);

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

schemaobjecttable = myschema->Elements; if (schemaobjecttable->Contains(ParentElementQName)) {       ParentElement = static_cast<XmlSchemaElement*>( schemaobjecttable->get_Item(ParentElementQName));

XmlSchemaKey *element_key = new XmlSchemaKey; element_key->Name  = S&quot;IDKey&quot;; element_key->Selector = new XmlSchemaXPath; element_key->Selector->XPath = S&quot;Children/Child&quot;;

XmlSchemaXPath *field = new XmlSchemaXPath; field->XPath = S&quot;@name&quot;; element_key->Fields->Add(field); ParentElement->Constraints->Add(element_key); }

myschema->Compile(veh);

//Write to an external file. StreamWriter *strmWriter = new StreamWriter(S&quot;c:\\NewSchema.xsd&quot;); myschema->Write(strmWriter); fstream->Close; strmWriter->Close; //Write to the 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); } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> <li>Expand Configuration Properties, and then click General.</li> <li>Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> <li>Press F5 to build and to run the application.</li></ol>

In the Output window, notice that the XSD has a new element that appears as follows: <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.

back to the top

<div class="references_section">