XmlReaderSettings.ValidationType Vlastnost
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Získá nebo nastaví hodnotu určující, zda XmlReader bude provádět ověření nebo přiřazení typu při čtení.
public:
property System::Xml::ValidationType ValidationType { System::Xml::ValidationType get(); void set(System::Xml::ValidationType value); };
public System.Xml.ValidationType ValidationType { get; set; }
member this.ValidationType : System.Xml.ValidationType with get, set
Public Property ValidationType As ValidationType
Hodnota vlastnosti
Jedna z ValidationType hodnot, které označují, zda XmlReader provede ověření nebo přiřazení typu při čtení. Výchozí hodnota je ValidationType.None.
Příklady
Následující příklad ověří pomocí schématu uloženého v souboru XmlSchemaSet.
using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public class Sample
{
public static void Main() {
// Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet();
// Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd");
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += ValidationCallBack;
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);
// Parse the file.
while (reader.Read());
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e) {
Console.WriteLine($"Validation Error:\n {e.Message}\n");
}
}
// The example displays output like the following:
// Validation Error:
// The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
// in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
// namespace 'urn:bookstore-schema'.
//
// Validation Error:
// The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
// in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
// namespace 'urn:bookstore-schema'.
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Public Module Sample
Public Sub Main()
' Create the XmlSchemaSet class.
Dim sc as XmlSchemaSet = new XmlSchemaSet()
' Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd")
' Set the validation settings.
Dim settings as XmlReaderSettings = new XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
' Create the XmlReader object.
Dim reader as XmlReader = XmlReader.Create("booksSchemaFail.xml", settings)
' Parse the file.
While reader.Read()
End While
End Sub
' Display any validation errors.
Private Sub ValidationCallBack(sender as object, e as ValidationEventArgs)
Console.WriteLine($"Validation Error:{vbCrLf} {e.Message}")
Console.WriteLine()
End Sub
End Module
' The example displays output like the following:
' Validation Error:
' The element 'book' in namespace 'urn:bookstore-schema' has invalid child element 'author'
' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'title' in
' namespace 'urn:bookstore-schema'.
'
' Validation Error:
' The element 'author' in namespace 'urn:bookstore-schema' has invalid child element 'name'
' in namespace 'urn:bookstore-schema'. List of possible elements expected: 'first-name' in
' namespace 'urn:bookstore-schema'.
Ukázka používá následující vstupní soubory:
booksSchemaFail.xml
<?xml version='1.0'?>
<bookstore xmlns="urn:bookstore-schema">
<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>
<book genre="philosophy">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.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>
Poznámky
Následující tabulka popisuje ValidationType hodnoty.
| Validationtype | Description |
|---|---|
DTD |
Ověření se provádí pomocí definice typu dokumentu (DTD). Poznámka: Vlastnost DtdProcessing musí být nastavena také na Parsehodnotu . |
None |
Neověřuje XmlReader data ani neprovádí přiřazení typu. |
Schema |
Ověření a přiřazení typu se provádí pomocí schématu XSD (XML Schema Definition Language). Čtenář přistupuje ke schématu XML pomocí následujícího: - Používá Schemas vlastnost pro přístup k objektu přidruženému XmlSchemaSet k tomuto čtenáři. – Používá vložené schéma obsažené v dokumentu instance XML. (Musí být povolená ProcessInlineSchema možnost.) - Používá schéma XML určené nápovědou k umístění schématu ( xsi:schemaLocation nebo xsi:noNamespaceSchemaLocation atributu) nalezené v dokumentu instance XML. (Musí být povolená ProcessSchemaLocation možnost.) |