How to create a key element for an XML schema in Visual C++ .NET or in Visual C++ 2005 (816171)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft Visual C++ .NET (2002)
- 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 TASKSUMMARYThis 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
topCreate an XML SchemaIn Notepad or a text editor of your choice, paste the following
code: <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Parent">
<xs:complexType>
<xs:sequence>
<xs:element name="Children">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
</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 topCreate a Visual C++ .NET or Visual C++ 2005 Application- Create a new Visual C++ .NET or Visual C++ 2005 application. To do this,
follow these steps:
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- 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. - Type XMLKey in the
Name text box, and then click OK.
- 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; - Add a reference to System.dll and to System.XML.dll. To do this, follow these steps:
Microsoft Visual Studio .NET
2002:
Microsoft Visual Studio .NET 2003:
- In Solution Explorer, right-click
References under XMLKey, and then click
Add Reference.
- 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.
- When both components appear under Selected Components, click OK.
Microsoft Visual Studio 2005:
- In Solution Explorer, right-click
XMLKey, click Reference, and then click
Add New Reference in the XMLKey Property Page dialog box.
- 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.
The components now appear under References in Solution Explorer. - 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);
}
}; - Add the following code in the int _tmain() function:
try
{
ValidationEventHandler *veh = new ValidationEventHandler(0,&MyClass::MyValidationEventHandler);
FileStream *fstream = new FileStream(S"C:\\key.xsd", FileMode::Open);
XmlSchema *myschema = new XmlSchema();
myschema = XmlSchema::Read(fstream,NULL);
myschema->Compile(veh);
XmlSchemaObjectTable *schemaobjecttable;
XmlSchemaElement *ParentElement;
XmlQualifiedName *ParentElementQName = new XmlQualifiedName(S"Parent", S"");
schemaobjecttable = myschema->Elements;
if (schemaobjecttable->Contains(ParentElementQName))
{
ParentElement = static_cast<XmlSchemaElement*>( schemaobjecttable->get_Item(ParentElementQName));
XmlSchemaKey *element_key = new XmlSchemaKey();
element_key->Name = S"IDKey";
element_key->Selector = new XmlSchemaXPath();
element_key->Selector->XPath = S"Children/Child";
XmlSchemaXPath *field = new XmlSchemaXPath();
field->XPath = S"@name";
element_key->Fields->Add(field);
ParentElement->Constraints->Add(element_key);
}
myschema->Compile(veh);
//Write to an external file.
StreamWriter *strmWriter = new StreamWriter(S"c:\\NewSchema.xsd");
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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site: - Press F5 to build and to run the
application.
In the Output window, notice that the XSD has a new element that
appears as follows: <xs:key name="IDKey">
<xs:selector xpath="Children/Child" />
<xs:field xpath="@name" />
</xs:key>
Additionally, the NewSchema.xsd file is created in the root
folder. back to the
topREFERENCESFor additional information about the XmlSchemaKeyref class, visit the following Microsoft Web site: back to the
top
Modification Type: | Major | Last Reviewed: | 1/18/2006 |
---|
Keywords: | kbXML kbHOWTOmaster KB816171 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|