Custom validation of standard xml schema (xsd) type in .net

The problem that I have is this:

  1. I have a lot of XSD files generated by 3rd party to check the conformance of document types
  2. They use standard types (namely gYear and date) without realizing their real restrictions
  3. They also use hard-coded checks on their side that have more restrictions then these standard types used in XSD (i.e., spaces before or after the numbers for gYear are ok, but will fail on their side)
  4. Changing schemas is really not a way to go (XSDs are numerous and being updated frequently)

So, here's the question: what can I do to enforce more strict rules to validate some of standard types? I use .NET 3.5 (C#) and we have a wrapper around the standard XmlReader, so using 3rd party project isn't an option at all. I thought about loading additional schema file with some pre-defined types that will extend/override needed types, or somehow catch the moment when the needed type is validating the node and I can peek at the value of said node, but I'm not sure how to proceed.

Additional requirements: the validation process should be streaming (i.e. no XmlDocument et al).


Well, as far as I can tell, there really isn't any simple solution to this problem.

The best I could have come up with, is to modify the schema upon reading it to put in cache.

I simply remove the type="xs:gYear" and add <simpleType> sub-element (if needed):

            <xs:restriction base="xs:string">
                    <xs:pattern value="^(19|2\d)\d{2}$" />

Luckily, we have the infrastructure to translate the validation errors to human-readable errors, so there shouldn't be a confusion about the differences between the actual schema and the validation errors for these elements.

Also, a big note goes to the fact that every single type in XML Schema has facet whiteSpace set to collapse, so you cannot inherit from more suitable types such as gYear, short or decimal.

Need Your Help

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.