This post originated from an RSS feed registered with .NET Buzz
by Scott Hanselman.
Original Post: Validating XML schemas that have been xs:imported and have schemaLocation
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
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:
...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 theCurrent
AppDomain's current directory, NOT the directory the WSDL was located in!
So:
Wsdl w = new Wsdl();
w.Load(file.FullName);
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.