BUG: Type "###" is not declared in reference to local type of an included XSD Schema file (317611)



The information in this article applies to:

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

This article was previously published under Q317611

SYMPTOMS

The current implementation of XSD schema-validation support in the .NET framework does not convert the targerNamespace of the local referencing type of an included XSD (it does not support the "chameleon" include). For example, the following a.xsd file includes the b.xsd and c.xsd files:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 	targetNamespace="test">

	<xsd:include schemaLocation="b.xsd" />
	<xsd:include schemaLocation="c.xsd" />
</xsd:schema>
				
Inside b.xsd, a testType type is defined:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:simpleType name="testType">
		<xsd:restriction base="xsd:string">
			<xsd:enumeration value="test"/>
		</xsd:restriction>
	</xsd:simpleType>
</xsd:schema>
				
Inside c.xsd, test is declared with the testType type:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:element name="test" type="testType"/>             <!-- this fails, but it should not fail -->
</xsd:schema>
				
The following error message is returned with the a.xsd schema file:
"Type 'testType' is not declared." in c.xsd

RESOLUTION

To work around this problem, add a xmlns="test" namespace declaration to the c.xsd file, for example:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="test">
	<xsd:element name="test" type="testType" xmlns="test"/>              
</xsd:schema>
				

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 Problem

  1. Save the preceding a.xsd file in the c:\temp folder. Save the b.xsd and c.xsd files under the same folder as a.xsd.
  2. Create a C# console Project.
  3. Replace the code the is inside the Class1.cs file with following code:
    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.Schema;
    
    namespace ValidationTest
    {
    	class Sample
    	{
    		public static void Main()
    		{
    			try
    			{
    				XmlSchemaCollection sc = new XmlSchemaCollection();
    				sc.Add("test", "c:\\temp\\a.xsd");
                                    Console.WriteLine("No Schema error.");			
    			}
    			catch(XmlSchemaException ex)
    			{
    				Console.WriteLine("XSD schema Error: {0}", ex.Message);
    			}
    			
    			Console.Read();
    		}
    	}
    }
    					
  4. Compile and then run the project. In the console window, the following error message is returned:
    XSD schema Error: Type 'test' is not declared. An error occurred at file:///c:/temp/c.xsd(2, 3).
  5. Open the c.xsd file, and then add xmlns="test" to the xsd:element, for example:
    <xsd:element name="test" type="test" xmlns="test"/>
    					
  6. Compile and then run the project. In the console window, the following output is returned:
    No Schema error.

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbbug kbenv KB317611