VB.Net by API/System.Xml.Serialization/XmlValidatingReader
XmlValidatingReader.Schemas.Add
Imports System.Xml
Imports System.Xml.Schema
Public Class MainClass
Shared Dim valid As Boolean " validation result
Shared Sub Main()
Dim schemas As XmlSchemaCollection " Schemas
valid = True " assume document is valid
schemas = New XmlSchemaCollection()
schemas.Add("book", "book.xdr")
Dim reader As XmlTextReader = New XmlTextReader("document.xml")
Dim validator As XmlValidatingReader = New XmlValidatingReader(reader)
validator.Schemas.Add(schemas)
validator.ValidationType = ValidationType.XDR
AddHandler validator.ValidationEventHandler, AddressOf ValidationError
While validator.Read
End While
If valid Then
Console.WriteLine("Document is valid")
End If
valid = True " reset variable
validator.Close()
End Sub
Shared Private Sub ValidationError(ByVal sender As Object, ByVal arguments As ValidationEventArgs)
Console.WriteLine(arguments.Message)
valid = False
End Sub
End Class
XmlValidatingReader.ValidationEventHandler
Imports System.Xml
Imports System.Xml.Schema
Public Class MainClass
Shared Dim valid As Boolean " validation result
Shared Sub Main()
Dim schemas As XmlSchemaCollection " Schemas
valid = True " assume document is valid
schemas = New XmlSchemaCollection()
schemas.Add("book", "book.xdr")
Dim reader As XmlTextReader = New XmlTextReader("document.xml")
Dim validator As XmlValidatingReader = New XmlValidatingReader(reader)
validator.Schemas.Add(schemas)
validator.ValidationType = ValidationType.XDR
AddHandler validator.ValidationEventHandler, AddressOf ValidationError
While validator.Read
End While
If valid Then
Console.WriteLine("Document is valid")
End If
valid = True " reset variable
validator.Close()
End Sub
Shared Private Sub ValidationError(ByVal sender As Object, ByVal arguments As ValidationEventArgs)
Console.WriteLine(arguments.Message)
valid = False
End Sub
End Class
XmlValidatingReader.ValidationType
Imports System.Xml
Imports System.Xml.Schema
Public Class MainClass
Shared Dim valid As Boolean " validation result
Shared Sub Main()
Dim schemas As XmlSchemaCollection " Schemas
valid = True " assume document is valid
schemas = New XmlSchemaCollection()
schemas.Add("book", "book.xdr")
Dim reader As XmlTextReader = New XmlTextReader("document.xml")
Dim validator As XmlValidatingReader = New XmlValidatingReader(reader)
validator.Schemas.Add(schemas)
validator.ValidationType = ValidationType.XDR
AddHandler validator.ValidationEventHandler, AddressOf ValidationError
While validator.Read
End While
If valid Then
Console.WriteLine("Document is valid")
End If
valid = True " reset variable
validator.Close()
End Sub
Shared Private Sub ValidationError(ByVal sender As Object, ByVal arguments As ValidationEventArgs)
Console.WriteLine(arguments.Message)
valid = False
End Sub
End Class