.NET 2.0: XmlDocument and Validation

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Could someone give me a basic outline or point me to an article with a sufficient code sample to get me up to speed on how to validate an XmlDocument Ive read from file against a schema (.xsd) file that is also on disk? It seems this has been reworked in the DOM for the CLR 2.0 (the <span id="nsrTitle XmlValidatingReader class is now obsolete...).

Here is what I worked up based on the scanty help files available with VS 2005 Beta 2:

<table border="0" cellspacing="0" width="100% <tr><td width="15 </td><td bgcolor="lightgrey" width="15 </td><td bgcolor="lightgrey
<font face="Lucida Console" size="2 XmlSchemaSet schemaSet = <font color= "blue new</font> XmlSchemaSet();
schemaSet.Add("<nameSpace>", "<path to schema file>");
XmlDocument sessionDocument = <font color= "blue new</font> XmlDocument();
sessionDocument.Schemas = schemaSet;
sessionDocument.Load("<path to xml file>");

<span id="nsrTitle ValidationEventHandler eventHandler = <font color= "blue new</font> ValidationEventHandler(HandleValidationError);
sessionDocument.Validate(eventHandler);
</font>
</td></tr></table>

If I alter my XML file so that it violates the schema Im trying to validate it against here, it does not fire the delegate method <span id="nsrTitle HandleValidationError(). With other objects, etc. it is allowed to call methods similar to Validate (that accept a delegate like this) without the delegate parameter and it will just throw an exception. This method does not allow that, and this stinks as I can think of no good way to have code following the Validate call not execute if the document does not validate.

Any suggestions welcome. Thanks.


View the full article
 
Back
Top