How to validate XMLDocument (not XMLReader) against a DTD?

  • Thread starter Thread starter engwar
  • Start date Start date
E

engwar

The title says it all. How do I use XMLDocument.Validate() against a
DTD.

In all my Googling about this I only find examples of how to validate
an XMLReader or XmlValidatingReader against a DTD.

The reason I want to avoid the readers is that I want an XmlDocument
that is not forward-only so I can loop through the doc again after
validating it.

I want to do something like the following but I keep getting the error
"the XmlSchemaSet on the document is either null or has no schemas in
it. Provide schema information before calling Validate."

What am I doing wrong?

string myXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!DOCTYPE
book SYSTEM \"book.dtd"><book><title>I Robot</title><author>Asimov</
author></book>";
XmlDocument r = new XmlDocument();
r.LoadXml(myXml);
ValidationEventHandler eventHandler = new
ValidationEventHandler(ValidationEventHandler);
r.Validate(eventHandler);
 
engwar,

The XmlValidatingReader (which is obsolete, you should use the Create
method on the XmlReader class to create an XmlReader to do this for you)
derives from XmlReader. You can pass this reader to the Load method of the
XmlDocument class when it loads and have the reader do the validation for
you.

Hope this helps.
 
Back
Top