trouble resolving location in <xs:import > element in C#

Posted by BobC on Stack Overflow See other posts from Stack Overflow or by BobC
Published on 2009-05-01T20:48:07Z Indexed on 2010/03/31 20:03 UTC
Read the original article Hit count: 441

Filed under:
|
|
|
|

I'm using an XML schema document to validate incoming data documents, however the schema appears be failing during compilation at run time because it refers to a complex type which part of an external schema. The external schema is specified in a element at the top of the document. I had thought it might be an access problem, so I moved a copy of the external document to a localhost folder. I get the same error, so now I'm wondering if there might be some sort of issue with the use of the element.

The schema document fragment looks like this:

<xs:schema targetNamespace="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:cpl="http://www.smpte-ra.org/schemas/429-7/2006/CPL" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
...  
<xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd"/>
...
<xs:element name="Signer" type="ds:KeyInfoType" minOccurs="0"/>
...
</xs:schema>

The code I'm trying to run this with is real simple (got it from http://dotnetslackers.com/Community/blogs/haissam/archive/2008/11/06/validate-xml-against-xsd-xml-schema-using-c.aspx)

string XSDFILEPATH = @"http://localhost/TMSWebServices/XMLSchema/CPL.xsd";
string XMLFILEPATH = @"C:\foo\bar\files\TestCPLs\CPL_930f5e92-be03-440c-a2ff-a13f3f16e1d6.xml";

System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.Schemas.Add(null, XSDFILEPATH);
settings.ValidationType = System.Xml.ValidationType.Schema;

System.Xml.XmlDocument document = new System.Xml.XmlDocument();
document.Load(XMLFILEPATH);

System.Xml.XmlReader rdr = System.Xml.XmlReader.Create(new StringReader(document.InnerXml), settings);
while (rdr.Read()) 
{ 

}

Everything goes well until the line that instantiates the XMLReader object just before the while loop. Then it fails with a type not declared error. The type that it's trying to find, KeyInfoType, is defined in one of the the documents in the import element. I've made sure the namespaces line up. I wondered if the # signs in the namespace definitions were causing a problem, but removing them had no effect, it just changed what the error looked like (i.e. "Type 'http://www.w3.org/2000/09/xmldsig:KeyInfoType' is not declared." versus "Type 'http://www.w3.org/2000/09/xmldsig#:KeyInfoType' is not declared.")

My suspicion is that there's something about the processing of the element that I'm missing. Any suggestions are very welcome. Thanks!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about schema