XmlSchemaCollection.ValidationEventHandler Event
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Sets an event handler for receiving information about the XDR and XML schema validation errors.
public:
event System::Xml::Schema::ValidationEventHandler ^ ValidationEventHandler;
public event System.Xml.Schema.ValidationEventHandler ValidationEventHandler;
member this.ValidationEventHandler : System.Xml.Schema.ValidationEventHandler
The following example shows how to set an event handler to handle invalid XML Schemas.
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
public ref class Sample
{
private:
//Display the schema error information.
static void ValidationCallBack( Object^ sender, ValidationEventArgs^ args )
{
Console::WriteLine( "Invalid XSD schema: {0}", args->Exception->Message );
}
public:
static void main()
{
// Create the schema collection.
XmlSchemaCollection^ xsc = gcnew XmlSchemaCollection;
//Set an event handler to manage invalid schemas.
xsc->ValidationEventHandler += gcnew ValidationEventHandler( Sample::ValidationCallBack );
//Add the schema to the collection.
xsc->Add( nullptr, "invalid.xsd" );
}
};
int main()
{
Sample::main();
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public class Sample
{
public static void Main (){
//Create the schema collection.
XmlSchemaCollection xsc = new XmlSchemaCollection();
//Set an event handler to manage invalid schemas.
xsc.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
//Add the schema to the collection.
xsc.Add(null, "invalid.xsd");
}
//Display the schema error information.
private static void ValidationCallBack (object sender, ValidationEventArgs args){
Console.WriteLine("Invalid XSD schema: " + args.Exception.Message);
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
public class Sample
public shared sub Main ()
'Create the schema collection.
Dim xsc as XmlSchemaCollection = new XmlSchemaCollection()
'Set an event handler to manage invalid schemas.
AddHandler xsc.ValidationEventHandler, AddressOf ValidationCallBack
'Add the schema to the collection.
xsc.Add(nothing, "invalid.xsd")
end sub
'Display the schema error information.
Private shared sub ValidationCallBack (sender as object, args as ValidationEventArgs)
Console.WriteLine("Invalid XSD schema: " + args.Exception.Message)
end sub
end class
The preceding example uses the file invalid.xsd
as input.
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' >
<xsd:complexType name="personName">
<xsd:sequence>
<xsd:element name="title" minOccurs="0" maxOccurs="1"/>
<xsd:element name="forename" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="surname"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="simpleName">
<xsd:complexContent>
<xsd:restriction base="personName">
<xsd:sequence>
<xsd:element name="title" minOccurs="0" maxOccurs="0"/>
<xsd:element name="firstname" minOccurs="1" maxOccurs="1"/>
<xsd:element name="surname"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
These events occur when the schemas are added to the collection. If an event handler is not provided, an XmlSchemaException is thrown on any validation errors where the Severity is XmlSeverityType.Error
. To specify an event handler, define a callback function and add it to the ValidationEventHandler
.
Important
The XmlSchemaCollection class is obsolete in the .NET Framework version 2.0 and has been replaced by the XmlSchemaSet class.
Product | Versions |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: