ValidationEventArgs::Severity Property

 

Gets the severity of the validation event.

Namespace:   System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public:
property XmlSeverityType Severity {
	XmlSeverityType get();
}

Property Value

Type: System.Xml.Schema::XmlSeverityType

An XmlSeverityType value representing the severity of the validation event.

The following example validates an XML file and generates the appropriate error or warning.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

ref class Sample
{
private:
	static void Validate(String^ filename, XmlSchemaSet^ schemaSet)
    {
        Console::WriteLine();
        Console::WriteLine("\r\nValidating XML file {0}...", filename->ToString());

        XmlSchema^ compiledSchema;

        for each (XmlSchema^ schema in schemaSet->Schemas())
        {
            compiledSchema = schema;
        }

        XmlReaderSettings^ settings = gcnew XmlReaderSettings();
        settings->Schemas->Add(compiledSchema);
        settings->ValidationEventHandler += gcnew ValidationEventHandler(ValidationCallBack);
		settings->ValidationType = ValidationType::Schema;

        //Create the schema validating reader.
		XmlReader^ vreader = XmlReader::Create(filename, settings);

        while (vreader->Read()) { }

        //Close the reader.
        vreader->Close();
    }

	//Display any warnings or errors.
    static void ValidationCallBack(Object^ sender, ValidationEventArgs^ args)
    {
		if (args->Severity == XmlSeverityType::Warning)
            Console::WriteLine("\tWarning: Matching schema not found.  No validation occurred." + args->Message);
        else
			Console::WriteLine("\tValidation error: " + args->Message);
    }

public:
    static void Main()
    {
        //Load the XmlSchemaSet.
        XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
        schemaSet->Add("urn:bookstore-schema", "books.xsd");

        //Validate the file using the schema stored in the schema set.
        //Any elements belonging to the namespace "urn:cd-schema" generate
        //a warning because there is no schema matching that namespace.
        Validate("store.xml", schemaSet);
        Console::ReadLine();
    }
};

int main()
{
	Sample::Main();
	return 0;
}

The preceding example uses the following input files.

store.xml

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema" xmlns:cd="urn:cd-schema">
  <book genre="novel">
    <title>The Confidence Man</title>
    <price>11.99</price>
  </book>
  <cd:cd>
    <title>Americana</title>
    <cd:artist>Offspring</cd:artist>
    <price>16.95</price>
  </cd:cd>
</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>

.NET Framework
Available since 1.1
Return to top
Show: