
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Validating Modified XML Documents Without Reloading
|
867
{
Console.WriteLine("Validation Error Line Number: {0}",
e.Exception.LineNumber);
Console.WriteLine("Validation Error Line Position: {0}",
e.Exception.LinePosition);
Console.WriteLine("Validation Error Source: {0}",
e.Exception.Source);
Console.WriteLine("Validation Error Source Schema: {0}",
e.Exception.SourceSchemaObject);
Console.WriteLine("Validation Error Source Uri: {0}",
e.Exception.SourceUri);
Console.WriteLine("Validation Error thrown from: {0}",
e.Exception.TargetSite);
Console.WriteLine("Validation Error callstack: {0}",
e.Exception.StackTrace);
}
}
Discussion
There is an override to the XmlDocument.Validate method that allows you to pass a
specific
XmlNode to validate. If the XmlDocument is large, this override to Validate
should be used:
public void Validate(
ValidationEventHandler validationEventHandler,
XmlNode nodeToValidate
);
One other approach to this problem is to instantiate an instance of the XmlNodeReader
with the XmlDocument and then create an XmlReader with validation settings as shown
in Recipe 15.4. This would allow for continual validation while the reader navigated
through the underlying XML.
The output from running the code is listed here:
Validation Error Message: The element 'Book' in namespace ...