[Mono-bugs] [Bug 496192] New: XmlSchema error: The instance type was not found while reading a XML file

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Fri Apr 17 17:07:07 EDT 2009


           Summary: XmlSchema error: The instance type was not found while
                    reading a XML file
    Classification: Mono
           Product: Mono: Class Libraries
           Version: unspecified
          Platform: i686
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Sys.XML
        AssignedTo: atsushi at ximian.com
        ReportedBy: stu_dan at hispeed.cH
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

Created an attachment (id=286601)
 --> (http://bugzilla.novell.com/attachment.cgi?id=286601)
The demo project

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; de; rv:
Gecko/2009032711 Ubuntu/8.10 (intrepid) Firefox/3.0.8


The attached project works fine under Microsoft .NET but fails under Mono with
the following error information:
Unhandled Exception: System.Xml.Schema.XmlSchemaValidationException: XmlSchema
error: The instance type was not found: HotelAddress XML  Line 5, Position 6.

After several hours of trial and error and some debugging I found a workaround.
More about the workaround later in this message.

I'm not sure but the problematic lines of code can be found in the method
XmlQualifiedName.Parse, which is called from the XmlSchemaValidator method
GetXsiType. First I thought it's the lookup of the type done in the method
FindType, cause in my demo program the GlobalTypes table had no entry. But this
was fixed after compiling the the Schemas with:
and in fact it is not necessary to compile the schemas.

A closer look at the XmlQualifiedName.Parse method made me curious. There is a
namespace lookup for prefixed types, but if there is no prefix, as in my demo
project, the index is < 0 and the qualified name with an empty namespaces is
returned (line: return new XMlQualifiedName (name)).

In my opinion a qualified name always needs a namespace (otherwise it's not a
qualified one) and so I suggest to have the following line:
return new XmlQualifiedName (name, resolver.LookupNamespace("")); 

-- Workaround --
Cause there is a lookup for the namespace by the prefix in XmlQualfiedName, you
just have to prefix the elements and xsi-types in the XML file. i.e.

<?xml version="1.0" encoding="utf-8" ?>

<tns:Person xmlns:tns="http://tempuri.org/Person.xsd" Firstname="Daniel"


To write such an XML file you need to specifiy the namespace for the
serializer. i.e.
serializer.Serialize(Console.Out, person, new XmlSerializerNamespaces(new
XmlQualifiedName[] { new XmlQualifiedName("tns",
"http://tempuri.org/Person.xsd"), new XmlQualifiedName("xsi",
"http://www.w3.org/2001/XMLSchema-instance") })); 

-- XmlSchemaValidator.cs --
private XmlSchemaType FindType (XmlQualifiedName qname)
    return (XmlSchemaType) schemas.GlobalTypes [qname];

private object GetXsiType (string name)
    object xsiType = null;
    XmlQualifiedName typeQName =
        XmlQualifiedName.Parse (name, nsResolver);
    if (typeQName == ComplexType.AnyTypeName)
        xsiType = ComplexType.AnyType;
    else if (XmlSchemaUtil.IsBuiltInDatatypeName (typeQName))
        xsiType = XsDatatype.FromName (typeQName);
        xsiType = FindType (typeQName);
    return xsiType;

-- XmlQualifiedName.cs --
internal static XmlQualifiedName Parse (string name, IXmlNamespaceResolver
    int index = name.IndexOf (':');
    if (index < 0)
        return new XmlQualifiedName (name);
    string ns = resolver.LookupNamespace (name.Substring (0, index));
    if (ns == null)
        throw new ArgumentException ("Invalid qualified name.");
    return new XmlQualifiedName (name.Substring (index + 1), ns);

-- Full error information --
Unhandled Exception: System.Xml.Schema.XmlSchemaValidationException: XmlSchema
error: The instance type was not found: HotelAddress XML  Line 5, Position 6.
  at System.Xml.XmlReaderSettings.OnValidationError (System.Object o,
System.Xml.Schema.ValidationEventArgs e) [0x00000] 
(System.Object o, System.Xml.Schema.ValidationEventArgs e) [0x00000] 
  at System.Xml.Schema.XmlSchemaValidator.HandleError
(System.Xml.Schema.XmlSchemaValidationException exception, Boolean isWarning)
  at System.Xml.Schema.XmlSchemaValidator.HandleError (System.String message,
System.Exception innerException, Boolean isWarning) [0x00000] 
  at System.Xml.Schema.XmlSchemaValidator.HandleError (System.String message)
  at System.Xml.Schema.XmlSchemaValidator.HandleXsiType (System.String
typename) [0x00000] 
  at System.Xml.Schema.XmlSchemaValidator.ValidateElement (System.String
localName, System.String ns, System.Xml.Schema.XmlSchemaInfo info,
System.String xsiType, System.String xsiNil, System.String schemaLocation,
System.String noNsSchemaLocation) [0x00000] 
  at Mono.Xml.Schema.XmlSchemaValidatingReader.Read () [0x00000] 
  at System.Xml.XmlReader.MoveToContent () [0x00000] 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadListElement
(System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable,
System.Object list, Boolean canCreateInstance) [0x00000] 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers
(System.Xml.Serialization.ClassMap map, System.Object ob, Boolean isValueList,
Boolean readByOrder) [0x00000] 
(System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob) [0x00000] 
(System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean
checkType) [0x00000] 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject
(System.Xml.Serialization.XmlTypeMapping typeMap, Boolean isNullable, Boolean
checkType) [0x00000] 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot
(System.Xml.Serialization.XmlTypeMapping rootMap) [0x00000] 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot ()
  at System.Xml.Serialization.XmlSerializer.Deserialize
(System.Xml.Serialization.XmlSerializationReader reader) [0x00000]

Reproducible: Always

Steps to Reproduce:
1. Compile and run
Actual Results:  
Works under Microsoft .NET
Fails under Mono

Expected Results:  
Works under Microsoft .NET
Works under Mono

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list