BUG: XmlSchema Rearranges the Positions of Annotation and Include Elements (317707)



The information in this article applies to:

  • Microsoft .NET Framework Class Libraries 1.1
  • Microsoft .NET Framework Class Libraries 1.0

This article was previously published under Q317707

SYMPTOMS

XmlSchema rearranges the positions of xs:annotation and xs:include schema elements when those elements are intermingled in an XML schema so that all of the xs:include elements are brought to the beginning.

STATUS

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

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Paste the following code in a text file, and then save the file as C:\Q317707.xsd. Note that this schema contains several annotations intermingled with some includes that reference external schema files:
    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myURN="urn:myURN">
       <xs:annotation>
             <xs:documentation>First child; annotation for xs:schema</xs:documentation>
       </xs:annotation>
       <xs:annotation>
             <xs:documentation>Second child; annotation for xs:schema</xs:documentation>
       </xs:annotation>
       <xs:include schemaLocation="Q317707External1.xsd">
          <xs:annotation>
             <xs:documentation>
             Third child; annotation for the first xs:include element
             </xs:documentation>
          </xs:annotation>
       </xs:include>
       <xs:annotation>
             <xs:documentation>Fourth child; annotation for xs:schema</xs:documentation>
       </xs:annotation>
       <xs:include schemaLocation="Q317707External2.xsd">
          <xs:annotation>
             <xs:documentation>
             Fifth child; annotation for the second xs:include element
             </xs:documentation>
          </xs:annotation>
       </xs:include>
       <xs:element name="myElement1">
          <xs:annotation>
                <xs:documentation>Sixth child; annotation for an element</xs:documentation>
          </xs:annotation>
       </xs:element>
       <xs:element name="myElement2"/>
    </xs:schema>
    					
  2. Paste the following two code sections in text files, and then save the files as C:\Q317707External1.xsd and C:\Q317707External2.xsd. These are the two external schema files that are referenced in the Q317707.xsd schema:
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myURN="urn:myURN" >
       <xs:element name="test1" type="xs:string"/>
    </xs:schema>
    					
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:myURN="urn:myURN" >
       <xs:element name="test2" type="xs:string"/>
    </xs:schema>
    					
  3. In Microsoft Visual Studio .NET, create a new Visual C# Console Application project. Replace the original code in the Class1.cs file with the following code:
    using System;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace Q317707
    {
       public class Class1
       {
          // Event handler for handling compilation messages.
          public static void ValidationCallback(object sender, ValidationEventArgs args) 
          {
             Console.WriteLine(args.Message);
          }
       
          public static void Main(string[] args)
          {
             try
             {      
                // Load the schema file.
                XmlTextReader tr = new XmlTextReader("c:\\Q317707.xsd");
                XmlSchema schema = XmlSchema.Read(tr, new ValidationEventHandler(ValidationCallback));
    
                // Compile the Schema; no errors should occur during this step.
                schema.Compile(new ValidationEventHandler(ValidationCallback));
    
                // Write out the schema which shows the rearrangement.
                schema.Write(Console.Out); 
                Console.WriteLine();       
             }
             catch(XmlSchemaException schemaExcep)
             { 
                Console.WriteLine(schemaExcep.Message);
             }
             catch(Exception excep)
             { 
                Console.WriteLine(excep);
             }
          }
       }
    }
    					
  4. Compile and run the application. The output resembles the following:
    <?xml version="1.0" encoding="IBM437"?>
    <xs:schema xmlns:myURN="urn:myURN" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:include schemaLocation="Q317707External1.xsd">
        <xs:annotation>
          <xs:documentation>
             Third child; annotation for the first xs:include element
             </xs:documentation>
        </xs:annotation>
      </xs:include>
      <xs:include schemaLocation="Q317707External2.xsd">
        <xs:annotation>
          <xs:documentation>
             Fifth child; annotation for the second xs:include element
             </xs:documentation>
        </xs:annotation>
      </xs:include>
      <xs:annotation>
        <xs:documentation>First child; annotation for xs:schema</xs:documentation>
      </xs:annotation>
      <xs:annotation>
        <xs:documentation>Second child; annotation for xs:schema</xs:documentation>
      </xs:annotation>
      <xs:annotation>
        <xs:documentation>Fourth child; annotation for xs:schema</xs:documentation>
      </xs:annotation>
      <xs:element name="myElement1">
        <xs:annotation>
          <xs:documentation>Sixth child; annotation for an element</xs:documentation>
        </xs:annotation>
      </xs:element>
      <xs:element name="myElement2" />
    </xs:schema>
    					
    Note that the sequence of the elements is changed and xs:include elements are located before the annotations. You expect the sequence to be the same as the sequence in the original schema file.

Modification Type:MajorLast Reviewed:9/22/2003
Keywords:kbbug kbpending KB317707