XmlValidatingReader::ValidationEventHandler Event
Sets an event handler for receiving information about document type definition (DTD), XML-Data Reduced (XDR) schema, and XML Schema definition language (XSD) schema validation errors.
Assembly: System.Xml (in System.Xml.dll)
Note |
|---|
The XmlValidatingReader class is obsolete in .NET Framework 2.0. You can create a validating XmlReader instance by using the XmlReaderSettings class and the Create method. For more information, see the Remarks section of the XmlReader reference page. |
These events occur during Read and only if a ValidationType of DTD, XDR, Schema, or Auto is specified.
If no event handler is provided, an XmlException is thrown on the first validation error (Severity is equal to XmlSeverityType.Error).
Note |
|---|
If an element reports a validation error, the rest of the content model for that element is not validated, however, its children are validated. The reader only reports the first error for a given element. |
The callback handler can use the ValidationEventArgs::Severity property to guarantee that an XML instance document is validated against a schema. The Severity property allows you to distinguish between validation errors (Severity is equal to XmlSeverityType.Error) that indicate a fatal error, and validation warnings (Severity is equal to XmlSeverityType.Warning) that indicate that no schema information is available.
The following example validates a file against an XML Schema.
#using <System.Xml.dll> using namespace System; using namespace System::IO; using namespace System::Xml; using namespace System::Xml::Schema; public ref class Sample { private: XmlTextReader^ txtreader; XmlValidatingReader^ reader; Boolean m_success; public: Sample() { txtreader = nullptr; reader = nullptr; m_success = true; //Validate file against the XSD schema. //The validation should fail. Validate( "notValidXSD.xml" ); } private: void Validate( String^ filename ) { try { Console::WriteLine( "Validating XML file {0}", filename ); txtreader = gcnew XmlTextReader( filename ); reader = gcnew XmlValidatingReader( txtreader ); // Set the validation event handler reader->ValidationEventHandler += gcnew ValidationEventHandler( this, &Sample::ValidationEventHandle ); // Read XML data while ( reader->Read() ) {} Console::WriteLine( "Validation finished. Validation {0}", (m_success == true ? (String^)"successful" : "failed") ); } finally { //Close the reader. if ( reader != nullptr ) reader->Close(); } } //Display the validation error. void ValidationEventHandle( Object^ /*sender*/, ValidationEventArgs^ args ) { m_success = false; Console::WriteLine( "\r\n\tValidation error: {0}", args->Message ); } }; int main() { gcnew Sample; }
The sample uses the following two input files:
notValidXSD.xml (The xsi:schemaLocation attribute identifies the XML Schema for the reader.)
<?xml version='1.0'?> <bookstore xmlns="urn:bookstore-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:bookstore-schema books.xsd"> <book> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> </book> <book genre="novel"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99</price> </book> </bookstore>
books.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema"> <xsd:element name="bookstore" type="bookstoreType"/> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="authorName"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string"/> <xsd:element name="last-name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
Available since 1.1
