VB.Net/XML/XML Validation

Материал из VB Эксперт
Перейти к: навигация, поиск

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.")
                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
                While validator.Read()
                End While
            Catch ex As Exception
                failed = True
            End Try
            Return Not failed
        End Function
        Private Sub HandleValidationEvents(ByVal sender As Object, ByVal args As ValidationEventArgs)
            failed = True
        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()
        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
            End If
            writer = editor.AppendChild()
            writer.WriteAttributeString("au_lname", "A")
            writer.WriteAttributeString("au_fname", "B")
        editor = doc.CreateNavigator()
        schemaSet = New System.Xml.Schema.XmlSchemaSet()
        schemaSet.Add(Nothing, "pubs.xsd")
        handler = New System.Xml.Schema.ValidationEventHandler(AddressOf ValidationCallback)
    End Sub
    Public Sub ValidationCallback(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)
    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("The ValidatingReader Failed with severity : {0}", _
        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>