views:

439

answers:

3

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"/&gt;
...
<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!

A: 

Sorry, there are a few places in my question where I put the <xs:import > element without properly formatting it, so they don't show up in the question

BobC
A: 

I think you need to add just one line of code to make it work:

settings.ValidationFlags =  
      System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;

Marc

marc_s
I tried that line, but with no effect. I looked at the ProcessSchemaLocation flag and found that the examples I've seen use a compound operator (new to me): settings.ValidationFlags |= System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;Unfortunately that didn't work either. I'll keep plugging away at it, I guess. Thanks!
BobC
A: 

Ok, this is getting a bit baffling. I've tried to do this a few different ways including adding the line:

settings.ValidationFlags |= System.Xml.Schema.XmlSchemaValidationFlags.ProcessSchemaLocation;

and I keep getting the same error:

Type 'http://www.w3.org/2000/09/xmldsig#:KeyInfoType' is not declared.

The document specified by that namespace is:

http://localhost/TMSWebServices/XMLSchema/xmldsig-core-schema.xsd

The document is accessible from where I am and I can locate the (seemingly) offending KeyInfoType type at line 152.

Just for fun, I examined the document being validated and found that the element defined in the schema of this type is not located in the document. The schema defines it as optional (minOccurs="0"), so that's not the issue.

It's almost as though there's something weird about the framework's ability to compile a schmea document when that document imports external schema documents. Has anyone seen this behavior? Google has not proven fruitful for this problem, although it did give me a bunch of suggestions to try. Thanks!

BobC