XmlDocument.Validate Metoda

Definice

Ověří proti XmlDocument schématům XML Schema Definition Language (XSD) obsaženým Schemas ve vlastnosti.

Přetížení

Validate(ValidationEventHandler)

Ověří proti XmlDocument schématům XML Schema Definition Language (XSD) obsaženým Schemas ve vlastnosti.

Validate(ValidationEventHandler, XmlNode)

Ověří XmlNode zadaný objekt podle schémat XML Schema Definition Language (XSD) ve Schemas vlastnosti.

Validate(ValidationEventHandler)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Ověří proti XmlDocument schématům XML Schema Definition Language (XSD) obsaženým Schemas ve vlastnosti.

public:
 void Validate(System::Xml::Schema::ValidationEventHandler ^ validationEventHandler);
public void Validate (System.Xml.Schema.ValidationEventHandler? validationEventHandler);
public void Validate (System.Xml.Schema.ValidationEventHandler validationEventHandler);
member this.Validate : System.Xml.Schema.ValidationEventHandler -> unit
Public Sub Validate (validationEventHandler As ValidationEventHandler)

Parametry

validationEventHandler
ValidationEventHandler

Objekt ValidationEventHandler , který přijímá informace o upozorněních a chybách ověření schématu.

Výjimky

Došlo k události ověření schématu a nebyl zadán žádný ValidationEventHandler objekt.

Příklady

Následující příklad znázorňuje použití Validate metody. Příklad vytvoří objekt XmlDocument , který obsahuje přidružené schéma XSD pomocí XmlReaderSettings objektů a XmlReader . Příklad pak použije XPathNavigator třídu k nesprávné úpravě zadané hodnoty elementu v dokumentu XML, který generuje chybu ověření schématu.

#using <System.Xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::Xml::XPath;

class XPathValidation
{
public:

    static void Main()
    {
        try
        {
            XmlReaderSettings^ settings = gcnew XmlReaderSettings();
            settings->Schemas->Add("http://www.contoso.com/books", "contosoBooks.xsd");
            settings->ValidationType = ValidationType::Schema;

            XmlReader^ reader = XmlReader::Create("contosoBooks.xml", settings);
            XmlDocument^ document = gcnew XmlDocument();
            document->Load(reader);

            ValidationEventHandler^ eventHandler = gcnew ValidationEventHandler(ValidationEventHandlerOne);

            // the following call to Validate succeeds.
            document->Validate(eventHandler);

            // add a node so that the document is no longer valid
            XPathNavigator^ navigator = document->CreateNavigator();
            navigator->MoveToFollowing("price", "http://www.contoso.com/books");
            XmlWriter^ writer = navigator->InsertAfter();
            writer->WriteStartElement("anotherNode", "http://www.contoso.com/books");
            writer->WriteEndElement();
            writer->Close();

            // the document will now fail to successfully validate
            document->Validate(eventHandler);
        }
        catch(Exception^ ex)
        {
            Console::WriteLine(ex->Message);
        }
    }

    static void ValidationEventHandlerOne(Object^ sender, ValidationEventArgs^ e)
    {
        switch (e->Severity)
        {
        case XmlSeverityType::Error:
            Console::WriteLine("Error: {0}", e->Message);
            break;
        case XmlSeverityType::Warning:
            Console::WriteLine("Warning {0}", e->Message);
            break;
        }

    }
};

int main()
{
    XPathValidation::Main();
    Console::ReadLine();
    return 0;
};
using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;

class XPathValidation
{
    static void Main()
    {
        try
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
            settings.ValidationType = ValidationType.Schema;

            XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
            XmlDocument document = new XmlDocument();
            document.Load(reader);

            ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);

            // the following call to Validate succeeds.
            document.Validate(eventHandler);

            // add a node so that the document is no longer valid
            XPathNavigator navigator = document.CreateNavigator();
            navigator.MoveToFollowing("price", "http://www.contoso.com/books");
            XmlWriter writer = navigator.InsertAfter();
            writer.WriteStartElement("anotherNode", "http://www.contoso.com/books");
            writer.WriteEndElement();
            writer.Close();

            // the document will now fail to successfully validate
            document.Validate(eventHandler);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    static void ValidationEventHandler(object sender, ValidationEventArgs e)
    {
        switch (e.Severity)
        {
            case XmlSeverityType.Error:
                Console.WriteLine("Error: {0}", e.Message);
                break;
            case XmlSeverityType.Warning:
                Console.WriteLine("Warning {0}", e.Message);
                break;
        }
    }
}
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.XPath

Class XPathValidation

    Shared Sub Main()

        Try

            Dim settings As XmlReaderSettings = New XmlReaderSettings()
            settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd")
            settings.ValidationType = ValidationType.Schema

            Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)
            Dim document As XmlDocument = New XmlDocument()
            document.Load(reader)

            Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)

            ' the following call to Validate succeeds.
            document.Validate(eventHandler)

            ' add a node so that the document is no longer valid
            Dim navigator As XPathNavigator = document.CreateNavigator()
            navigator.MoveToFollowing("price", "http://www.contoso.com/books")
            Dim writer As XmlWriter = navigator.InsertAfter()
            writer.WriteStartElement("anotherNode", "http://www.contoso.com/books")
            writer.WriteEndElement()
            writer.Close()

            ' the document will now fail to successfully validate
            document.Validate(eventHandler)

        Catch ex As Exception

            Console.WriteLine(ex.Message)

        End Try

    End Sub

    Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)

        Select Case e.Severity
            Case XmlSeverityType.Error
                Console.WriteLine("Error: {0}", e.Message)
            Case XmlSeverityType.Warning
                Console.WriteLine("Warning {0}", e.Message)
        End Select

    End Sub

End Class

V příkladu se jako vstup vezmou contosoBooks.xml soubory a contosoBooks.xsd .

<?xml version="1.0" encoding="utf-8" ?>
<bookstore xmlns="http://www.contoso.com/books">
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
        <title>The Confidence Man</title>
        <author>
            <first-name>Herman</first-name>
            <last-name>Melville</last-name>
        </author>
        <price>11.99</price>
    </book>
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
        <title>The Gorgias</title>
        <author>
            <name>Plato</name>
        </author>
        <price>9.99</price>
    </book>
</bookstore>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookstore">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="book">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string" />
                            <xs:element name="author">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element minOccurs="0" name="name" type="xs:string" />
                                        <xs:element minOccurs="0" name="first-name" type="xs:string" />
                                        <xs:element minOccurs="0" name="last-name" type="xs:string" />
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                            <xs:element name="price" type="xs:decimal" />
                        </xs:sequence>
                        <xs:attribute name="genre" type="xs:string" use="required" />
                        <xs:attribute name="publicationdate" type="xs:date" use="required" />
                        <xs:attribute name="ISBN" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Poznámky

Metoda Validate ověřuje data XML v objektu XmlDocument proti schématům obsaženým ve Schemas vlastnosti. Metoda Validate provádí rozšíření infoset. Konkrétně se po úspěšném ověření použijí výchozí hodnoty schématu, textové hodnoty se podle potřeby převedou na atomické hodnoty a informace o typu jsou přidruženy k ověřeným položkám informací. Výsledkem je dříve nezadávkovaný pod strom XML v pod stromě XmlDocument , který byl nahrazen zadaným pod stromem.

Následující důležité poznámky je potřeba při použití Validate metody zvážit.

  • Nápovědy k umístění schématu, jako xsi:schemaLocation jsou nebo xsi:noNamespaceSchemaLocation , se ignorují.

  • Vložená schémata se ignorují.

  • Pokud během ověřování dojde k chybám ověření schématu, XmlDocument bude se částečně ověřit u některých uzlů se správnými informacemi o typu a u některých bez.

  • Proces ověření zahrnuje kontrolu jedinečnosti a omezení odkazů (xs:ID, xs:IDREF, xs:keyxs:keyrefa xs:unique).

Platí pro

Validate(ValidationEventHandler, XmlNode)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Ověří XmlNode zadaný objekt podle schémat XML Schema Definition Language (XSD) ve Schemas vlastnosti.

public:
 void Validate(System::Xml::Schema::ValidationEventHandler ^ validationEventHandler, System::Xml::XmlNode ^ nodeToValidate);
public void Validate (System.Xml.Schema.ValidationEventHandler? validationEventHandler, System.Xml.XmlNode nodeToValidate);
public void Validate (System.Xml.Schema.ValidationEventHandler validationEventHandler, System.Xml.XmlNode nodeToValidate);
member this.Validate : System.Xml.Schema.ValidationEventHandler * System.Xml.XmlNode -> unit
Public Sub Validate (validationEventHandler As ValidationEventHandler, nodeToValidate As XmlNode)

Parametry

validationEventHandler
ValidationEventHandler

Objekt ValidationEventHandler , který přijímá informace o upozorněních a chybách ověření schématu.

nodeToValidate
XmlNode

Objekt XmlNode vytvořený z objektu XmlDocument k ověření.

Výjimky

Parametr XmlNode objektu nebyl vytvořen z objektu XmlDocument.

Parametr XmlNode object není element, atribut, fragment dokumentu ani kořenový uzel.

Došlo k události ověření schématu a nebyl zadán žádný ValidationEventHandler objekt.

Příklady

Příklad Validate metody najdete v Validate metodě.

Poznámky

Metoda Validate ověřuje data XML v objektu XmlNode proti schématům obsaženým ve Schemas vlastnosti. Metoda Validate provádí rozšíření infoset. Konkrétně se po úspěšném ověření použijí výchozí hodnoty schématu, textové hodnoty se podle potřeby převedou na atomické hodnoty a informace o typu jsou přidruženy k ověřeným položkám informací. Výsledkem je dříve nezadávkovaný pod strom XML v pod stromě XmlDocument , který byl nahrazen zadaným pod stromem.

Následující důležité poznámky je potřeba při použití Validate metody zvážit.

  • Nápovědy k umístění schématu, jako xsi:schemaLocation jsou nebo xsi:noNamespaceSchemaLocation , se ignorují.

  • Vložená schémata se ignorují.

  • Pokud během ověřování dojde k chybám ověření schématu, XmlDocument bude se částečně ověřit u některých uzlů se správnými informacemi o typu a u některých bez.

Pokud je uzlem, který se má ověřit, kořenový uzel, proces ověření zahrnuje kontrolu jedinečnosti a omezení odkazů (xs:ID, xs:IDREFxs:key, xs:keyrefa xs:unique), jinak se vynechávají omezení jedinečnosti a odkazu.

Platí pro