XmlValidatingReader.ValidationEventHandler イベントとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlValidatingReader.ValidationEventHandler イベントの意味・解説 

XmlValidatingReader.ValidationEventHandler イベント

ドキュメント型定義 (DTD)、XML-Data Reduced (XDR) スキーマ、および XML スキーマ定義言語 (XSD) スキーマ検証エラーに関する情報受信するためのイベント ハンドラ設定します

名前空間: System.Xml
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Public Event ValidationEventHandler As
 ValidationEventHandler
Dim instance As XmlValidatingReader
Dim handler As ValidationEventHandler

AddHandler instance.ValidationEventHandler, handler
public event ValidationEventHandler ValidationEventHandler
public:
event ValidationEventHandler^ ValidationEventHandler {
    void add (ValidationEventHandler^ value);
    void remove (ValidationEventHandler^ value);
}
/** @event */
public void add_ValidationEventHandler (ValidationEventHandler
 value)

/** @event */
public void remove_ValidationEventHandler (ValidationEventHandler
 value)
JScript では、イベント使用できますが、新規に宣言することはできません。
解説解説
使用例使用例

XML スキーマファイル検証する例を次に示します

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.VisualBasic

Public Class Sample
    
    Private txtreader As XmlTextReader = Nothing
    Private reader As XmlValidatingReader =
 Nothing
    Private m_success As Boolean
 = True
    
    Public Sub New()
        'Validate file against the XSD schema. 
        'The validation should fail.
        Validate("notValidXSD.xml")
    End Sub 'New
    
    Public Shared Sub Main()
        Dim validation As New
 Sample()
    End Sub 'Main
    
    Private Sub Validate(filename As
 String)
        Try
            Console.WriteLine("Validating XML file "
 & filename.ToString())
            txtreader = New XmlTextReader(filename)
            reader = New XmlValidatingReader(txtreader)
            
            ' Set the validation event handler
            AddHandler reader.ValidationEventHandler, AddressOf
 Me.ValidationEventHandle
            
            ' Read XML data
            While reader.Read()
            End While
            Console.WriteLine("Validation finished. Validation
 {0}", IIf(m_success, "successful", "failed"))
        
        Finally
            'Close the reader.
            If Not (reader Is
 Nothing) Then
                reader.Close()
            End If
        End Try
    End Sub 'Validate
     
    'Display the validation error.
    Private Sub ValidationEventHandle(sender
 As Object, args As ValidationEventArgs)
        m_success = False
        Console.WriteLine(ControlChars.CrLf & ControlChars.Tab & "Validation
 error: " & args.Message)
    End Sub 'ValidationEventHandle
End Class 'Sample
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Sample
{

  private XmlTextReader txtreader = null;
  private XmlValidatingReader reader = null;
  private Boolean m_success = true;

  public Sample ()
  {
        //Validate file against the XSD schema. 
        //The validation should fail.
        Validate("notValidXSD.xml"); 
  }    

  public static void Main
 ()
  {
      Sample validation = new Sample();
  }

  private void Validate(String filename)
  {    
     try
     {
        Console.WriteLine("Validating XML file " + filename.ToString());
        txtreader = new XmlTextReader (filename);
        reader = new XmlValidatingReader (txtreader);

        // Set the validation event handler
        reader.ValidationEventHandler += new ValidationEventHandler
 (this.ValidationEventHandle);

        // Read XML data
        while (reader.Read()){}
        Console.WriteLine ("Validation finished. Validation {0}", (m_success==true
 ? "successful" : "failed"));
     }

     finally
     {
        //Close the reader.
        if (reader != null)
          reader.Close();
     } 
  }

  //Display the validation error.
  private void ValidationEventHandle (object
 sender, ValidationEventArgs args)
  {
     m_success = false;
     Console.WriteLine("\r\n\tValidation error: " + args.Message );
  }
}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
public ref class Sample
{
private:
   XmlTextReader^ txtreader;
   XmlValidatingReader^ reader;
   Boolean m_success;

public:
   Sample()
   {
      txtreader = nullptr;
      reader = nullptr;
      m_success = true;
      
      //Validate file against the XSD schema. 
      //The validation should fail.
      Validate( "notValidXSD.xml" );
   }


private:
   void Validate( String^ filename )
   {
      try
      {
         Console::WriteLine( "Validating XML file {0}", filename );
         txtreader = gcnew XmlTextReader( filename );
         reader = gcnew XmlValidatingReader( txtreader );
         
         // Set the validation event handler
         reader->ValidationEventHandler += gcnew ValidationEventHandler( this,
 &Sample::ValidationEventHandle );
         
         // Read XML data
         while ( reader->Read() )
         {}
         Console::WriteLine( "Validation finished. Validation {0}", (m_success
 == true ? (String^)"successful" : "failed")
 );
      }
      finally
      {
         
         //Close the reader.
         if ( reader != nullptr )
                  reader->Close();
      }

   }


   //Display the validation error.
   void ValidationEventHandle( Object^ /*sender*/, ValidationEventArgs^
 args )
   {
      m_success = false;
      Console::WriteLine( "\r\n\tValidation error: {0}", args->Message
 );
   }

};

int main()
{
   gcnew Sample;
}

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Schema.*;

public class Sample
{
    private XmlTextReader txtReader = null;
    private XmlValidatingReader reader = null;
    private boolean mSuccess = true;

    public Sample()
    {
        //Validate file against the XSD schema. 
        //The validation should fail.
        Validate("notValidXSD.xml");
    } //Sample

    public static void main(String[]
 args)
    {
        Sample validation =  new Sample();
    } //main

    private void Validate(String filename)
    {
        try {
            Console.WriteLine(("Validating XML file " + filename.ToString()));
            txtReader = new XmlTextReader(filename);
            reader = new XmlValidatingReader(txtReader);

            // Set the validation event handler
            reader.add_ValidationEventHandler(new ValidationEventHandler
                (this.ValidationEventHandle));

            // Read XML data
            while(reader.Read()) {

            }
            Console.WriteLine("Validation finished. Validation {0}",
                (mSuccess == true) ? "successful" :
 "failed");
        }
        finally {
            //Close the reader.
            if (reader != null) {
                reader.Close();
            }
        }
    } //Validate

    //Display the validation error.
    private void ValidationEventHandle(Object
 sender, ValidationEventArgs args) 
    {
        mSuccess = false;
        Console.WriteLine(("\r\n\tValidation error: " + args.get_Message()));
    } //ValidationEventHandle
} //Sample

このサンプルでは、次の 2 つ入力ファイル使用します

notValidXSD.xml (xsi:schemaLocation 属性は、リーダーXML スキーマ識別します)

<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="urn:bookstore-schema books.xsd">
  <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>
</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>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlValidatingReader クラス
XmlValidatingReader メンバ
System.Xml 名前空間
XmlSeverityType
その他の技術情報
XmlReader による XML読み取り



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlValidatingReader.ValidationEventHandler イベントのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlValidatingReader.ValidationEventHandler イベントのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS