次の方法で共有


XmlSchemaCollection.ValidationEventHandler イベント

XDR スキーマ検証エラー、および XSD スキーマ検証エラーに関する情報を受信するためのイベント ハンドラを設定します。

Public Event ValidationEventHandler As ValidationEventHandler
[C#]
public event ValidationEventHandlerValidationEventHandler;
[C++]
public: __event ValidationEventHandler* ValidationEventHandler;

[JScript] JScript では、このクラスで定義されているイベントを処理できます。ただし、独自に定義することはできません。

イベント データ

イベント ハンドラが、このイベントに関連するデータを含む、ValidationEventArgs 型の引数を受け取りました。次の ValidationEventArgs プロパティには、このイベントの固有の情報が記載されます。

プロパティ 説明
Exception 検証イベントに関連付けられている XmlSchemaException を取得します。
Message 検証イベントに対応している説明テキストを取得します。
Severity 検証イベントの重大度レベルを取得します。

解説

これらのイベントは、スキーマをコレクションに追加するときに発生します。イベント ハンドラが提供されない場合、 XmlSchemaException は、 SeverityXmlSeverityType.Error である任意の検証エラーにスローされます。イベント ハンドラを指定するには、コールバック関数を定義し、それを ValidationEventHandler に追加します。

使用例

無効な XML スキーマを処理するイベント ハンドラの設定方法の例を次に示します。

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

[C#] 
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);
  }  
}

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;

__gc public class Sample
{
public:

   //Display the schema error information.
   static void ValidationCallBack (Object* sender, ValidationEventArgs * args)
   {
      Console::WriteLine(S"Invalid XSD schema: {0}", args -> Exception -> Message);
   }  
};

int main()
{
   // Create the schema collection.
   XmlSchemaCollection* xsc = new XmlSchemaCollection();

   //Set an event handler to manage invalid schemas.
   xsc -> ValidationEventHandler += new ValidationEventHandler (xsc, Sample::ValidationCallBack);

   //Add the schema to the collection.  
   xsc -> Add(0, S"invalid.xsd");
}    

[JScript] 
import System
import System.IO
import System.Xml
import System.Xml.Schema

public class Sample
{

  public static function Main (){

    //Create the schema collection.
    var xsc : XmlSchemaCollection = new XmlSchemaCollection();

    //Set an event handler to manage invalid schemas.
    xsc.add_ValidationEventHandler(ValidationCallBack);

    //Add the schema to the collection.  
    xsc.Add(null, "invalid.xsd");

  }    

  //Display the schema error information.
  private static function ValidationCallBack(sender, args : ValidationEventArgs){
     Console.WriteLine("Invalid XSD schema: " + args.Exception.Message);
  }  

}

前述の例では、入力として、 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>

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XmlSchemaCollection クラス | XmlSchemaCollection メンバ | System.Xml.Schema 名前空間