I'm absolutely tearing my hair out on this one, and would appreciate
any help anyone can provide. I need to validate incoming XML files
against a local XSD which imports a remote XSD. When setting up the
XmlReaderSettings, I am getting a XmlSchemaException on
XmlSchemaSet.Compile(). I have stripped down my setup to the very
basics below.
(1) REMOTE XSD FILE: Available at http://localhost/ImportedSchema.xsd
<?xml version="1.0" encoding="utf-8"?>
<s:schema id="ImportedSchema" targetNamespace="urn:schemas-me-
com:Test" elementFormDefault="qualified" xmlns="urn:schemas-me-
com:Test" xmlns:tns="urn:schemas-me-com:Test" xmlns:s="http://
www.w3.org/2001/XMLSchema">
<s:complexType name="TestType">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="SomeValue"
type="s:string" />
</s:sequence>
</s:complexType>
</s:schema>
(2) LOCAL XSD FILE: Available in output directory
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="LocalSchema" targetNamespace="urn:schemas-me-
com:TestLocal" elementFormDefault="qualified" xmlns="urn:schemas-me-
com:TestLocal" xmlns:mstns="urn:schemas-me-com:TestLocal"
xmlns:imp="urn:schemas-me-com:Test" xmlns:xs="http://www.w3.org/2001/
XMLSchema">
<xs:import id="imp" namespace="urn:schemas-me-com:Test"
schemaLocation="http://localhost/ImportedSchema.xsd" />
<xs:element name="LocalStuff">
<xs:complexType>
<xs:sequence>
<xs:element name="LocalElement" type="xs:string" maxOccurs="1"
minOccurs="1" />
<xs:element name="ImportedElement" type="imp:TestType"
maxOccurs="1" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
(3) CODE trying setup the XmlReaderSettings
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, "LocalSchema.xsd");
schemaSet.Compile(); // BANG! XmlSchemaException thrown!
// ... etc ...
What is really strange is that Visual Studio is resolving everything
perfectly within the IDE. if I set up a test XML file, I'm getting
intellisense on the imported types and everything. Here's a test XML
file that is working fine:
<?xml version="1.0" encoding="utf-8" ?>
<LocalStuff
xmlns="urn:schemas-me-com:TestLocal"
xmlns:imp="urn:schemas-me-com:Test">
<LocalElement>Hello</LocalElement>
<ImportedElement>
<imp:SomeValue>World</imp:SomeValue>
</ImportedElement>
</LocalStuff>
Any ideas?
Dave
iHadMyPersonalInfoIndexed@gmail.com - 28 Jun 2007 21:41 GMT
Solved the problem. My localhost was using Integrated Windows
Authentication. I had to specify the credentials for the XmlResolver:
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;
schemaSet.XmlResolver = resolver;
Hope it helps someone else!