Convalida di XML Schema (XSD) con più schemi
È possibile utilizzare XmlValidatingReader per convalidare documenti XML rispetto a schemi del linguaggio XSD (XML Schema Definition) da più schemi. Il costruttore di XmlValidatingReader accetta un flusso di un frammento XML, un XmlNodeType e XmlParserContext.
Nota Nell'esempio che segue l'elemento di primo livello non è convalidato. La proprietà XmlValidatingReader genera un avviso per l'elemento principale se ValidationType è impostato su Schema.
L'esempio di codice che segue crea un XmlValidatingReader che accetta un flusso, Mixed.xml, come input e convalida frammenti XML rispetto a più origini di schemi.
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Namespace ValidationSample
Class Sample
Public Shared Sub Main()
Dim stream As New FileStream("Mixed.xml", FileMode.Open)
Dim vr As New XmlValidatingReader(stream, XmlNodeType.Element, Nothing)
vr.Schemas.Add(Nothing, "Book.xsd")
vr.Schemas.Add(Nothing, "Tape.xsd")
vr.ValidationType = ValidationType.Schema
AddHandler vr.ValidationEventHandler, AddressOf ValidationHandler
While vr.Read()
End While
Console.WriteLine("Validation finished")
End Sub
' Main
Public Shared Sub ValidationHandler(sender As Object, args As ValidationEventArgs)
Console.WriteLine("***Validation error")
Console.WriteLine("Severity:{0}", args.Severity)
Console.WriteLine("Message:{0}", args.Message)
End Sub
' ValidationHandler
End Class
' Sample
End Namespace
' ValidationSample
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace ValidationSample
{
class Sample
{
public static void Main()
{
FileStream stream = new FileStream("Mixed.xml", FileMode.Open);
XmlValidatingReader vr = new XmlValidatingReader(stream, XmlNodeType.Element, null);
vr.Schemas.Add(null, "Book.xsd");
vr.Schemas.Add(null, "Tape.xsd");
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
while(vr.Read());
Console.WriteLine("Validation finished");
}
public static void ValidationHandler(object sender, ValidationEventArgs args)
{
Console.WriteLine("***Validation error");
Console.WriteLine("\tSeverity:{0}", args.Severity);
Console.WriteLine("\tMessage:{0}", args.Message);
}
}
}
Di seguito viene indicato il contenuto del file di uno schema XML, Book.xsd, rispetto al quale eseguire la convalida.
<xs:schema xmlns="urn:bookstore-schema"
targetNamespace="urn:bookstore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="book">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="price" type="xs:decimal" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Di seguito viene indicato il contenuto del file di uno schema XML, Tape.xsd, rispetto al quale eseguire la convalida.
<xs:schema xmlns="urn:tapestore-schema"
targetNamespace="urn:tapestore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tape" type="xs:string"/>
</xs:schema>
Di seguito viene indicato il contenuto del flusso XML, Mixed.xml, contenente i frammenti XML da convalidare.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="urn:dvdstore-schema" targetNamespace="urn:dvdstore-schema">
<xs:element name="dvd" type="xs:string" />
</xs:schema>
<pb:book price="7.99" xmlns:pb="urn:bookstore-schema">The Autobiography of Benjamin Franklin</pb:book>
<pd:dvd xmlns:pd="urn:dvdstore-schema">The Godfather</pd:dvd>
<pt:tape xmlns:pt="urn:tapestore-schema" xsi:schemaLocation="urn:tapestore-schema tape.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">Il Postino</pt:tape>
Vedere anche
Convalida del linguaggio XML con XmlValidatingReader | Convalida del linguaggio XML con schemi