Xml validating reader

Posted by / 12-Nov-2016 16:41

On this project, it's a (light) requirement that we use the specification schemas unchanged. So, I pass in a custom resolver that will find the correct schema given the URI (the value in the schema Location attribute) and return it, in this example, as a stream.

Here we just grab the relative filename from out of the file:/// URI that we're passed into Get Entity each time a schema Location needs to be resolved. I wrap the whole thing in a factory method and cache the compiled Xml Schema Collection so we don't load and compile this more than once. I've seen folks build Assembly schemas like assembly:/// and embed stuff in the schemas, but eh, who has the time.

A click on the 'Validate' button will validate the content of the XML instance editor against the schema from the XML schema viewer.

As you can modify the content of the instance editor, different instances can be tested and validation error messages analysed.

I added all the schemas to the project, right clicked "Properties" and set them all to Embedded Resources.

When you request an embedded resource you need to ask for the file using the original file name as well as the namespace.

Use Reflector to determine what the ultimate fully qualified resource name is if you have trouble.

It's easy to pull the main schema out of it's resource and pass the Stream into Xml Schema. It's slightly less obvious how to get that schema to resolve its imports.

I don't like to bake too much knowledge about the outside world into my schemas. The Xml Schema class will use a File System Resolver by default and fail to find the other 63 schemas.

If the schema is invalid or a parsing error has occurred, is Valid() returns .

If the loading was successful, a QXml Schema Validator is instanciated and the schema passed in the constructor.

Schemas may reference other schemas like this: In this, and most, cases schema Location refers to a relative file.

However it could refer to a URL, or some custom scheme.

xml validating reader-66xml validating reader-53xml validating reader-3

Registration only takes a minute and is of course perfectly free.