Blogger :
Oleg Tkachenko
All posts :
All posts by Oleg Tkachenko
Category :
.NET XML, System.XML
Blogged date : 2005 Oct 10
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.