October 10, 2005

ValidationType.Auto is obsolete, what do I do?

In .NET 2.0 ValidationType.Auto value is made obsolete. What's worse - it doesn't work for XmlReaders created via XmlReader.Create() factory method. But how do you validate against either DTD and/or schema, i.e. against DTD if document has a DOCTYPE and/or schema if any is applicable? The answer is: you can chain two XmlReaders, one set up for performing DTD validation and second - schema validation.

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
XmlReader inner = XmlReader.Create("book.xml", settings); // DTD Validation
settings.Schemas.Add("urn:book-schema", "book.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader outer = XmlReader.Create(inner, settings);  // XML Schema Validation
That's so intuitive and clean, but I'm sure many won't figure it out the first time. Shame on me, but I didn't. So I write this to save somebody his time. You know, .NET used to suck on chaining XmlReaders, but not anymore.

October 10, 2005 12:36 AM | #System.Xml v2.0 , #XML , #XML in .NET
Comments

I agree with the above post, but the fact is:
Please still use the obsolete version of DTD-Validation!
The one with XmlReaderSettings DOESN'T work for DTD!
It's a known bug that it currently only supports schema!

Posted by: Ynos at May 23, 2006 2:20 PM
Post a comment




Remember Me?