Scott Hanselman

Validating XML schemas that have been xs:imported and have schemaLocation

July 12, 2004 Comment on this post [4] Posted in Web Services | XML
Sponsored By

In this example, Wsdl is a class that's one of our internal convenience wrappers around System.Web.Services.Description.ServiceDescription.  The Schemas property is just the XmlSchema[] that from service.Types.Schemas. 

I wasn't sure if my WSDL file was kosher, and I wanted to "validate" the whole setup.  Most of my XML Schemas were pulled in via xsd:import in my wsdl like:

  <xsd:schema targetNamespace="" xmlns:SOAP-ENC="" xmlns:xsd="" xmlns:xsd1=""> <!-- bogus namespaces for example -->
   <xsd:import namespace="" schemaLocation="..\banking\BankingDomain.xsd"/>

...and as you can see, the schemaLocation was relative.  The way I was going to force a Validation of the XML Schemas (other than physically loading each of them into memory myself) was to call .Compile().  However, it appears that the relative paths were being resolved relative to the Current AppDomain's current directory, NOT the directory the WSDL was located in!  So:

Wsdl w = new Wsdl();
foreach(XmlSchema x in w.Schemas)
  foreach(XmlSchemaImport i in x.Includes)
i.SchemaLocation = Path.Combine(file.DirectoryName,i.SchemaLocation);
  x.Compile(new ValidationEventHandler(OnValidationEvent), new XmlUrlResolver());

Before I mess with the WSDL and Schemas, I take the directory that the WSDL file was located in and use it as a base directory for the relative schemaLocation.  Thank goodness the SchemaLocation property was not readonly! :)  Using Path.Combine has the added benefit of collapsing ".." relative paths.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
July 13, 2004 12:01
I think the same also could be achieved providing custom XmlResolver to the XmlSchema.Compile(ValidationEventHandler, XmlResolver) method.
July 13, 2004 14:23
What is the purpose of XmlSchema.Compile? Is it to validate the schema itself?
July 15, 2004 9:39
Very nice !!!! MORE ON THIS SUBJECT!
July 30, 2004 11:36
Nice link... I have posted it on my site.

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.