XmlReaderSettings::ValidationType Property
Gets or sets a value indicating whether the XmlReader will perform validation or type assignment when reading.
Assembly: System.Xml (in System.Xml.dll)
public: property ValidationType ValidationType { ValidationType get(); void set(ValidationType value); }
Property Value
Type: System.Xml::ValidationTypeOne of the ValidationType values that indicates whether XmlReader will perform validation or type assignment when reading. The default is ValidationType.None.
The following table describes the ValidationType values.
ValidationType | Description | ||
|---|---|---|---|
DTD | Validation is performed using a document type definition (DTD).
| ||
None | The XmlReader does not validate data, or perform any type assignment. | ||
Schema | Validation and type assignment is performed using an XML Schema definition language (XSD) schema. The reader accesses the XML Schema using the following:
|
The following example validates using a schema stored in the XmlSchemaSet.
#using <System.Xml.dll> using namespace System; using namespace System::Xml; using namespace System::Xml::Schema; using namespace System::IO; // Display any validation errors. static void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ e ) { Console::WriteLine( L"Validation Error: {0}", e->Message ); } int main() { // Create the XmlSchemaSet class. XmlSchemaSet^ sc = gcnew XmlSchemaSet; // Add the schema to the collection. sc->Add( L"urn:bookstore-schema", L"books.xsd" ); // Set the validation settings. XmlReaderSettings^ settings = gcnew XmlReaderSettings; settings->ValidationType = ValidationType::Schema; settings->Schemas = sc; settings->ValidationEventHandler += gcnew ValidationEventHandler( ValidationCallBack ); // Create the XmlReader object. XmlReader^ reader = XmlReader::Create( L"booksSchemaFail.xml", settings ); // Parse the file. while ( reader->Read() ) ; return 1; }
The sample uses the following input files:
booksSchemaFail.xml
<?xml version='1.0'?> <bookstore xmlns="urn:bookstore-schema"> <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> <book genre="philosophy"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.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 2.0
