My experiences with XmlReader class have been kind of mixed right now. I do want to clarify some of the basic doubts which I have with validating the Xml data using XmlReader class.
- I know one can use the XmlReaderSettings class to set up some settings as to whether to turn on/off validation, setting the validation type, etc.. and create an XmlReader with the xml file and the settings object.
- I also know that you need to add all your schema files used for validation into the XmlSchemaSet class unless the schema is present inside the Xml document (inline schema)
There is a property in XmlSchemaValidationFlags class called as XmlSchemaValidationFlags.ProcessSchemaLocation
So, if I do have the location of the xsd file specified in the xml file given below, do I need to add the xsd file to the XmlSchemaSet class? I have kept the xsd file in the same dir as of the xml file.
<?xml version="1.0" encoding="utf-8"?> <value_tables xmlns="urn:values-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:values-schema values.xsd" > <table name="Calibrated"> <value num="0">No</value> <value num="1">Yes</value> </table> </value_tables>
Q2. Also, is it possible to retrieve the name of this file from the xml document rather than hardcoding it?
You'd normally need to supply a Resolver (google EntityResolver) which tells the parser where to find the externally referenced documents. Yes, you could let the resolver take the location info directly from the namespace URI (but this is not recommended; I think I remember some XBRL taxonomies use this approach IIRC).
There may or may not be default EntityResolver implementations that do simple things (like: look in a single directory or download from the URI as a http url), but in my experience I always had to code an EntityResolve one way or another. It is not a lot of work anyway
Just a word of advice. As the validator of XML input data, you definitively should control the schema source. How else would you know what you are validating against?
There are use cases where the schemaLocation attribute is useful. For instance, the Spring framework uses different URLs for the same namespace to let the user specify either default or version specific schema support for configuration files. But the URLs are always symbolic and points indirectly to controlled schema related resources.
This, however, does not apply if the use case is to gain schema related information about the elements and attributes in the XML document.