ValidationType.Auto is obsolete, what do I do?

| 1 Comment | No TrackBacks

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.

Related Blog Posts

No TrackBacks

TrackBack URL: http://www.tkachenko.com/cgi-bin/mt-tb.cgi/510

1 Comment

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!

Leave a comment