共用方式為


XmlSchemaCollection.ValidationEventHandler 事件

定義

設定事件處理常式,接收關於 XDR 和 XML 結構描述驗證錯誤的資訊。

public:
 event System::Xml::Schema::ValidationEventHandler ^ ValidationEventHandler;
public event System.Xml.Schema.ValidationEventHandler ValidationEventHandler;
member this.ValidationEventHandler : System.Xml.Schema.ValidationEventHandler 

事件類型

範例

下列範例示範如何設定事件處理常式來處理不正確 XML 架構。

#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

上述範例會使用 檔案 invalid.xsd 做為輸入。

<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>

備註

當架構加入至集合時,就會發生這些事件。 如果未提供事件處理常式, XmlSchemaException 則會在 為 的任何驗證錯誤 SeverityXmlSeverityType.Error 上擲回 。 若要指定事件處理常式,請定義回呼函式,並將其新增至 ValidationEventHandler

重要

類別 XmlSchemaCollection 在 .NET Framework 2.0 版中已經過時,且已由 XmlSchemaSet 類別取代。

適用於