VB.Net/XML/XML Validation
Console Validator
Imports System
Imports System.Xml
Imports System.Xml.Schema
Public Class MainClass
Public Shared Sub Main(ByVal args As String())
Dim xmlValidator As New ConsoleValidator
Dim success As Boolean = xmlValidator.ValidateXML(args(0), args(1))
If Not success Then
Console.WriteLine("Validation failed.")
Else
Console.WriteLine("Validation succeeded.")
End If
End Sub
End Class
Public Class ConsoleValidator
Private failed As Boolean
Public Function ValidateXML(ByVal xmlFileName As String, ByVal schemaFileName As String)
Dim settings As New XmlReaderSettings
settings.ValidationType = ValidationType.Schema
Dim schemas As New XmlSchemaSet
settings.Schemas = schemas
schemas.Add(Nothing, schemaFileName)
AddHandler settings.ValidationEventHandler, AddressOf HandleValidationEvents
Dim validator As XmlReader = XmlReader.Create(xmlFileName, settings)
failed = False
Try
While validator.Read()
End While
Catch ex As Exception
Console.WriteLine(ex.Message)
failed = True
End Try
validator.Close()
Return Not failed
End Function
Private Sub HandleValidationEvents(ByVal sender As Object, ByVal args As ValidationEventArgs)
failed = True
Console.WriteLine(args.Message)
End Sub
End Class
Handle validation event
Public Class Example9
Shared Sub Main
Dim doc As System.Xml.XmlDocument
Dim editor, editor2 As System.Xml.XPath.XPathNavigator
Dim writer As System.Xml.XmlWriter
Dim schemaSet As System.Xml.Schema.XmlSchemaSet
Dim handler As System.Xml.Schema.ValidationEventHandler
doc = New System.Xml.XmlDocument()
doc.Load("pubs.xml")
For Each editor In doc.CreateNavigator().Select("/pubs/titles[authors/@au_lname="Green"]")
editor2 = editor.SelectSingleNode("authors[@au_lname!="Green"]")
If Not IsNothing(editor2) Then
editor2.DeleteSelf()
End If
writer = editor.AppendChild()
writer.WriteStartElement("authors")
writer.WriteAttributeString("au_lname", "A")
writer.WriteAttributeString("au_fname", "B")
writer.Close()
Next
editor = doc.CreateNavigator()
schemaSet = New System.Xml.Schema.XmlSchemaSet()
schemaSet.Add(Nothing, "pubs.xsd")
schemaSet.rupile()
handler = New System.Xml.Schema.ValidationEventHandler(AddressOf ValidationCallback)
doc.Save("output.xml")
End Sub
Public Sub ValidationCallback(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)
System.Console.WriteLine(e.Message)
End Sub
End Class
Validate XML document with Validation Error Handler
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.Data
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim xtr As New XmlTextReader("book.xml")
Dim xvr As New XmlValidatingReader(xtr)
xvr.ValidationType = ValidationType.Schema
AddHandler xvr.ValidationEventHandler, _
New ValidationEventHandler(AddressOf ValidationErrorHandler)
While xvr.Read()
If TypeOf xvr.SchemaType Is XmlSchemaComplexType Then
Console.WriteLine("{0} - {1}", xvr.NodeType, xvr.Name)
While xvr.MoveToNextAttribute()
Console.WriteLine(" {0} - {1}: {2}", _
xvr.NodeType, xvr.Name, xvr.Value)
End While
End If
End While
End Sub
Shared Sub ValidationErrorHandler(ByVal sender As Object, _
ByVal args As ValidationEventArgs)
Console.WriteLine("XML Document Validation Failure")
Console.WriteLine()
Console.WriteLine("The ValidatingReader Failed with severity : {0}", _
args.Severity)
Console.WriteLine("The failure message was: {0}", args.Message)
End Sub
End Class
"<?xml version = "1.0"?>
"<books xmlns = "x-schema:book.xdr">
" <book>
" <title>C# How to Program</title>
" </book>
" <book>
" <title>Java How to Program, 4/e</title>
" </book>
" <book>
" <title>Visual Basic .NET How to Program</title>
" </book>
" <book>
" <title>Advanced Java 2 Platform How to Program</title>
" </book>
" <book>
" <title>Python How to Program</title>
" </book>
"</books>