Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Initialisiert eine neue Instanz der XmlValidatingReader-Klasse, die den vom angegebenen XmlReader zurückgegebenen Inhalt validiert.
Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)
Syntax
'Declaration
Public Sub New ( _
reader As XmlReader _
)
'Usage
Dim reader As XmlReader
Dim instance As New XmlValidatingReader(reader)
public XmlValidatingReader (
XmlReader reader
)
public:
XmlValidatingReader (
XmlReader^ reader
)
public XmlValidatingReader (
XmlReader reader
)
public function XmlValidatingReader (
reader : XmlReader
)
Parameter
- reader
Der XmlReader, aus dem während der Überprüfung gelesen werden soll. Die aktuelle Implementierung unterstützt nur XmlTextReader.
Ausnahmen
| Ausnahmetyp | Bedingung |
|---|---|
Der angegebene Reader ist kein XmlTextReader. |
Hinweise
Hinweis
Die XmlValidatingReader-Klasse ist in Microsoft .NET Framework, Version 2.0 veraltet. Sie können eine Instanz eines validierenden XmlReader mithilfe der XmlReaderSettings-Klasse und der Create-Methode erstellen. Weitere Informationen finden Sie unter Validieren von XML-Daten mit "XmlReader".
Sämtliche vom angegebenen XmlReader zurückgegebenen Knoten werden auch vom überprüfenden Reader zurückgegeben, sodass in dem Prozess kein Informationsverlust eintritt. Neue Knoten, die nicht vom zugrunde liegenden Reader zurückgegeben wurden, können von diesem Reader hinzugefügt werden (z. B. Standardattribute und die untergeordneten Elemente eines Entitätsverweises). Sämtliche für einen angegebenen XmlTextReader festgelegten Eigenschaften werden auch auf diesen validierenden Reader angewendet. Wenn für den bereitgestellten Reader z. B. WhitespaceHandling.None festgelegt ist, ignoriert dieser überprüfende Reader Leerraum ebenfalls.
Wenn für die Validierung externe DTDs (Dokumenttypdefinitionen) oder Schemas erforderlich sind, legt die XmlResolver-Eigenschaft das XmlResolver-Objekt fest, mit dem externe Ressourcen aufgelöst werden sollen.
Beispiel
Im folgenden Beispiel werden zwei Dokumente überprüft.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.VisualBasic
public class Sample
private m_success as Boolean = true
public sub New ()
'Validate the document using an external XSD schema. Validation should fail.
Validate("notValidXSD.xml")
'Validate the document using an inline XSD. Validation should succeed.
Validate("inlineXSD.xml")
end sub
public shared sub Main ()
Dim validation as Sample = new Sample()
end sub
private sub Validate(filename as String)
m_success = true
Console.WriteLine()
Console.WriteLine("******")
Console.WriteLine("Validating XML file " + filename.ToString())
Dim txtreader as XmlTextReader = new XmlTextReader (filename)
Dim reader as XmlValidatingReader = new XmlValidatingReader (txtreader)
' Set the validation event handler
AddHandler reader.ValidationEventHandler, AddressOf ValidationCallBack
' Read XML data
while (reader.Read())
end while
Console.WriteLine ("Validation finished. Validation {0}", IIf(m_success, "successful!", "failed."))
'Close the reader.
reader.Close()
end sub
'Display the validation error.
Private sub ValidationCallBack (sender as object, args as ValidationEventArgs)
m_success = false
Console.WriteLine()
Console.WriteLine(" Validation error: " + args.Message )
end sub
end class
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
public class Sample
{
private Boolean m_success = true;
public Sample ()
{
//Validate the document using an external XSD schema. Validation should fail.
Validate("notValidXSD.xml");
//Validate the document using an inline XSD. Validation should succeed.
Validate("inlineXSD.xml");
}
public static void Main ()
{
Sample validation = new Sample();
}
private void Validate(String filename)
{
m_success = true;
Console.WriteLine("\r\n******");
Console.WriteLine("Validating XML file " + filename.ToString());
XmlTextReader txtreader = new XmlTextReader (filename);
XmlValidatingReader reader = new XmlValidatingReader (txtreader);
// Set the validation event handler
reader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
// Read XML data
while (reader.Read()){}
Console.WriteLine ("Validation finished. Validation {0}", (m_success==true ? "successful!" : "failed."));
//Close the reader.
reader.Close();
}
//Display the validation error.
private void ValidationCallBack (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:
static Boolean m_success = true;
public:
Sample()
{
// Validate the document using an external XSD schema. Validation should fail.
Validate( "notValidXSD.xml" );
// Validate the document using an inline XSD. Validation should succeed.
Validate( "inlineXSD.xml" );
}
private:
// Display the validation error.
void ValidationCallBack( Object^ /*sender*/, ValidationEventArgs^ args )
{
m_success = false;
Console::WriteLine( "\r\n\tValidation error: {0}", args->Message );
}
void Validate( String^ filename )
{
m_success = true;
Console::WriteLine( "\r\n******" );
Console::WriteLine( "Validating XML file {0}", filename );
XmlTextReader^ txtreader = gcnew XmlTextReader( filename );
XmlValidatingReader^ reader = gcnew XmlValidatingReader( txtreader );
// Set the validation event handler
reader->ValidationEventHandler += gcnew ValidationEventHandler( this, &Sample::ValidationCallBack );
// Read XML data
while ( reader->Read() )
{}
Console::WriteLine( "Validation finished. Validation {0}", (m_success == true ? (String^)"successful!" : "failed.") );
// Close the reader.
reader->Close();
}
};
int main()
{
Sample^ validation = gcnew Sample;
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Schema.*;
public class Sample
{
private boolean mSuccess = true;
public Sample()
{
// Validate the document using an external XSD schema.
// Validation should fail.
Validate("notValidXSD.xml");
//Validate the document using an inline XSD. Validation should succeed.
Validate("inlineXSD.xml");
} //Sample
public static void main(String[] args)
{
Sample validation = new Sample();
} //main
private void Validate(String fileName)
{
mSuccess = true;
Console.WriteLine("\r\n******");
Console.WriteLine("Validating XML file " + fileName.ToString());
XmlTextReader txtReader = new XmlTextReader(fileName);
XmlValidatingReader reader = new XmlValidatingReader(txtReader);
// Set the validation event handler
reader.add_ValidationEventHandler(
new ValidationEventHandler(ValidationCallBack));
// Read XML data
while (reader.Read()) {
}
Console.WriteLine("Validation finished. Validation {0}",
(mSuccess == true) ? "successful!" : "failed.");
//Close the reader.
reader.Close();
} //Validate
//Display the validation error.
private void ValidationCallBack(Object sender, ValidationEventArgs args)
{
mSuccess = false;
Console.WriteLine("\r\n\tValidation error: " + args.get_Message());
} //ValidationCallBack
} //Sample
Im Beispiel werden die folgenden Eingabedateien verwendet:
notValidXSD.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>
inlineXSD.xml
<store-data>
<!--Inline XSD schema-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<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="price" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
<!-- end of schema -->
<bookstore>
<book genre="novel">
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
</bookstore>
</store-data>
Plattformen
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
Siehe auch
Referenz
XmlValidatingReader-Klasse
XmlValidatingReader-Member
System.Xml-Namespace
XmlTextReader-Klasse