Microsoft KB Archive/316797

= FIX: The Validation of Keys Does Not Work If You Define More Than One Key =

Article ID: 316797

Article Last Modified on 9/24/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.0

-



This article was previously published under Q316797



SYMPTOMS
If you define multiple keys in the XML Schema Definition (XSD) language schema file, as in the following example               <xsd:selector xpath=&quot;event&quot; /> <xsd:field xpath=&quot;@name&quot; /> </xsd:key> </xsd:element> ... </xsd:complexType> you receive the following error message if you use the XmlValidatingReader class of the System.Xml namespace in .NET Framework to validate a XML file against the XSD file:

Exception Severity: Error

There is a duplicate key sequence 'InstanceCreated' for 'event-name-is-unique' key or unique identity constraint.

<div class="cause_section">

CAUSE
This problem occurs if the Identity Constraints on elements that are stored in the stack are not reset for later elements.

<div class="resolution_section">

RESOLUTION
To work around this problem, move the element with the constraints so that it appears last.

<div class="status_section">

STATUS
This bug was corrected in Microsoft .NET Framework Class Libraries 1.1.

<div class="moreinformation_section">

Steps to Reproduce the Problem
<ol> <li> Create a new XML file named Test.xml, and then add the following code: <jsml xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:noNamespaceSchemaLocation=&quot;test.xsd&quot;> <class name=&quot;Singleton&quot;> <event name=&quot;InstanceCreated&quot; scope=&quot;class&quot;> Fired when the singleton instance is created. <property name=&quot;instance&quot; scope=&quot;class&quot; type=&quot;Singleton&quot;> Singleton instance, initialized on first access. </li> <li>Save Test.xml to your computer's hard disk.</li> <li> Create a new .xsd file named Test.xsd, and then add the following code: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <xsd:schema elementFormDefault=&quot;qualified&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xsd:element name=&quot;jsml&quot; type=&quot;JsmlDocumentType&quot;> </xsd:element> <xsd:complexType name=&quot;JsmlDocumentType&quot;>  <xsd:element name=&quot;class&quot; type=&quot;ClassType&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;unbounded&quot;> <xsd:key name=&quot;class-name-is-unique&quot;> <xsd:selector xpath=&quot;class&quot; /> <xsd:field xpath=&quot;@name&quot; /> </xsd:key> </xsd:element> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot;ClassType&quot; mixed=&quot;false&quot;>  <xsd:element name=&quot;events&quot; type=&quot;EventsType&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot;> <xsd:key name=&quot;event-name-is-unique&quot;> <xsd:selector xpath=&quot;event&quot; /> <xsd:field xpath=&quot;@name&quot; /> </xsd:key> </xsd:element> <xsd:element name=&quot;property&quot; type=&quot;PropertyType&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot; /> </xsd:sequence> <xsd:attribute name=&quot;name&quot; type=&quot;xsd:string&quot; use=&quot;required&quot; /> <xsd:attribute name=&quot;inherits&quot; type=&quot;xsd:string&quot; use=&quot;optional&quot; /> </xsd:complexType> <xsd:complexType name=&quot;EventsType&quot; mixed=&quot;false&quot;>  <xsd:element name=&quot;event&quot; type=&quot;EventType&quot; minOccurs=&quot;1&quot; maxOccurs=&quot;unbounded&quot; /> </xsd:sequence> </xsd:complexType> <xsd:complexType name=&quot;EventType&quot; mixed=&quot;false&quot;>  <xsd:element name=&quot;desc&quot; type=&quot;DescriptionType&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; /> </xsd:sequence> <xsd:attribute name=&quot;name&quot; type=&quot;xsd:string&quot; use=&quot;required&quot; /> <xsd:attribute name=&quot;scope&quot; type=&quot;ScopeType&quot; use=&quot;required&quot; /> </xsd:complexType> <xsd:complexType name=&quot;PropertyType&quot; mixed=&quot;false&quot;>  <xsd:element name=&quot;desc&quot; type=&quot;xsd:string&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;1&quot; /> </xsd:sequence> <xsd:attributeGroup ref=&quot;IdentifierAttributeGroup&quot; /> </xsd:complexType>

<xsd:attributeGroup name=&quot;IdentifierAttributeGroup&quot;> <xsd:attribute name=&quot;name&quot; type=&quot;xsd:string&quot; use=&quot;required&quot; /> <xsd:attribute name=&quot;scope&quot; type=&quot;ScopeType&quot; use=&quot;required&quot; /> <xsd:attribute name=&quot;type&quot; type=&quot;xsd:string&quot; use=&quot;required&quot; /> </xsd:attributeGroup> <xsd:simpleType name=&quot;DescriptionType&quot;> <xsd:restriction base=&quot;xsd:string&quot; /> </xsd:simpleType> <xsd:simpleType name=&quot;ScopeType&quot;> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:enumeration value=&quot;class&quot; /> <xsd:enumeration value=&quot;instance&quot; /> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name=&quot;YesNoType&quot;> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:enumeration value=&quot;yes&quot; /> <xsd:enumeration value=&quot;no&quot; /> </xsd:restriction> </xsd:simpleType> </xsd:schema>

</li> <li>Save Test.xsd on drive C.</li> <li> Create a Microsoft Visual C# .NET console project, and then replace the code in Class1.cs with the following code: using System; using System.IO; using System.Xml; using System.Xml.Schema;

namespace ConsoleApplication1 {   ///     /// Summary description for Class1. ///    class Class1 {       ///         /// The main entry point for the application. ///        [STAThread] static void Main(string[] args) {           try {               XmlTextReader tr = new XmlTextReader(&quot;c:\\test.xml&quot;); XmlValidatingReader vr = new XmlValidatingReader(tr); vr.ValidationType = ValidationType.Schema; vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);

while(vr.Read); Console.Read; }           catch(XmlException xmlEx) {               Console.WriteLine(xmlEx.Message); Console.WriteLine(&quot;line: &quot; + xmlEx.LineNumber +&quot;\n&quot;+ &quot;src: &quot; + xmlEx.Source); Console.WriteLine(xmlEx.ToString); }           catch(Exception ex) {               Console.WriteLine(ex.Message+ &quot;\n&quot; + ex.ToString); }                  }        public static void ValidationHandler(object sender, ValidationEventArgs args) {           Console.WriteLine(&quot;***Validation error&quot;); Console.WriteLine(&quot;\tSeverity:{0}&quot;, args.Severity); Console.WriteLine(&quot;\tMessage :{0}&quot;, args.Message); }   } }

</li> <li>Compile the code, and then run it in debug mode:

You receive the following error message:


 * Validation error Severity:Error Message :There is a duplicate key sequence 'InstanceCreated' for 'event-name-is-unique' key or unique identity constraint. An error occurred at file:/ //c:/test.xml(5, 5).

</li></ol>

Keywords: kbbug kbpending KB316797

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.