BUG: You cannot validate an XML instance that references two schemas if the schemas circularly import each other (317490)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1

This article was previously published under Q317490
This article refers to the following Microsoft .NET Framework Class Library namespaces:
  • System.IO
  • System.Xml
  • System.Xml.Schema

SYMPTOMS

You receive a duplicate declaration error message under the following circumstances:
  • One Extensible Markup Language Schema Definition Language (XSD) schema (A.xsd) imports another XSD schema (B.xsd), and vice versa.
  • Both schemas are referenced in an Extensible Markup Language (XML) instance.
  • You add both schemas to the schema collection.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

This circular import problem also occurs in diamond include/imports wherein A.xsd includes A1.xsd, B.xsd includes B1.xsd, and B1.xsd imports A1.xsd. Both A.xsd and B.xsd are referenced by the same XML instance and added to the schema collection.

Steps to Reproduce the Behavior

  1. Create a new XSD schema named cir_imp.xsd that contains the following code:
    <xsd:schema targetNamespace="circular_import" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <xsd:import namespace="circular" schemaLocation="circular.xsd"/>
       <xsd:element name="B" type="xsd:string"/>
    </xsd:schema>
    					
  2. Create a new XSD schema named circular.xsd that contains the following code:
    <xsd:schema targetNamespace="circular" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <xsd:import namespace="circular_import" schemaLocation="cir_imp.xsd"/>
       <xsd:element name="A" type="xsd:string"/>
    </xsd:schema>
    					
  3. Create a new XML instance named Test.xml that contains the following code:
    <c1:A xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="circular circular.xsd" xmlns:c1="circular">
    testValue
    </c1:A>
    					
  4. Save cir_imp.xsd, circular.xsd, and Test.xml to drive C.
  5. Start Visual Studio .NET.
  6. Create a new Console Application in Microsoft Visual C# .NET.
  7. Replace the code in Class1.cs with the following code:
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace ValidationTest
    {
        class Test
        {
            static int _ValidationErrorsCount = 0;
    
            public static void Main()
            {
                try
                {
                    XmlTextReader tr = new XmlTextReader("c:\\cir.xml");
                    XmlValidatingReader vr = new XmlValidatingReader(tr);
    
                    vr.Schemas.Add("circular_import", "c:\\cir_imp.xsd");
                    vr.Schemas.Add("circular", "c:\\circular.xsd");
                    vr.ValidationType = ValidationType.Schema;
                    vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
    
                    while(vr.Read());
                    Console.WriteLine("Validation finished: {0} validation errors", _ValidationErrorsCount);
                }
                catch(System.Xml.Schema.XmlSchemaException ex)
                {
                    Console.WriteLine("Error {0}", ex.Message);
                }
            }
    
            public static void ValidationHandler(object sender, ValidationEventArgs args)
            {
                Console.WriteLine("***Validation error");
                Console.WriteLine("\tSeverity:{0}", args.Severity);
                Console.WriteLine("\tMessage  :{0}", args.Message);
                _ValidationErrorsCount++;
            }
        }
    }
    					
  8. Compile and run the project. Notice that the following error message appears in the console window:
    Error Global element 'circular:A' has already been declared. An error occurred a t file:///c:/circular.xsd(3, 5).

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbBug kbpending KB317490 kbAudDeveloper