XmlSchema Set Example : XML Schema « XML « VB.Net Tutorial






Imports System
Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaSetExample

    Shared Sub Main()

        Dim booksSettings As XmlReaderSettings = New XmlReaderSettings()
        booksSettings.Schemas.Add("http://www.yourname.com/books", "books.xsd")
        booksSettings.ValidationType = ValidationType.Schema
        AddHandler booksSettings.ValidationEventHandler, New ValidationEventHandler(AddressOf booksSettingsValidationEventHandler)

        Dim books As XmlReader = XmlReader.Create("books.xml", booksSettings)

        While books.Read()

        End While

    End Sub

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

        If e.Severity = XmlSeverityType.Warning Then
            Console.Write("WARNING: ")
            Console.WriteLine(e.Message)

        ElseIf e.Severity = XmlSeverityType.Error Then
            Console.Write("ERROR: ")
            Console.WriteLine(e.Message)
        End If

    End Sub

End Class


'<bookstore xmlns="http://www.yourname.com/books">
'  <book genre="autobiography" publicationdate="1999-12-12" ISBN="1-111111-11-1">
'    <title>A</title>
'    <author>
'      <first-name>X</first-name>
'      <last-name>Y</last-name>
'    </author>
'    <price>8.99</price>
'  </book>
'  <book genre="novel" publicationdate="2000-01-01" ISBN="0-201-63361-2">
'    <title>Java</title>
'    <author>
'      <first-name>Q</first-name>
'      <last-name>Q</last-name>
'    </author>
'    <price>11.99</price>
'  </book>
'  <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
'    <title>C#</title>
'    <author>
'      <name>Author</name>
'    </author>
'    <price>9.99</price>
'  </book>
'</bookstore>
'
'
'
'The following is the schema that validates the example XML document. 
'
'<?xml version="1.0" encoding="utf-8"?>
'<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.yourname.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>








25.5.XML Schema
25.5.1.Create XML Schema
25.5.2.Validating XML documents against Schemas
25.5.3.XmlSchema Set Example