Re: On the Fly XML Validation

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Joy wrote:

My XML is in string however my XSD will be in a file.

I am using VB .NET.

Here is an example class that writes validation warning, validation errors and well-formedness exceptions to the console:

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

Module TestValidation
Sub Main()
Dim xml As String = "<root><foo>1</foo></root>"
Dim schema As String = "..\..\XSDSchema1.xsd"
Dim validator As New ValidateXml(xml, schema)
Dim valid As Boolean = validator.Validate()
Console.WriteLine("{0} is valid: {1}.", xml, valid)

End Sub
End Module

Public Class ValidateXml

Private _valid As Boolean
Public Property Valid() As Boolean
Get
Return _valid
End Get
Set(ByVal value As Boolean)
_valid = value
End Set
End Property

Private _xml As String
Public Property Xml() As String
Get
Return _xml
End Get
Set(ByVal value As String)
_xml = value
End Set
End Property

Private _schemaFile As String
Public Property SchemaFile() As String
Get
Return _schemaFile
End Get
Set(ByVal value As String)
_schemaFile = value
End Set
End Property

Public Sub New(ByVal x As String, ByVal schema As String)
_xml = x
_schemaFile = schema
End Sub

Public Function Validate() As Boolean
_valid = True
Dim settings As New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = settings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, AddressOf ValidationHandler
settings.Schemas.Add(Nothing, _schemaFile)
Using reader As XmlReader = XmlReader.Create(New StringReader(_xml), settings)
Try
While reader.Read()
End While
Catch ex As XmlException
Console.WriteLine("Parse error: {0}", ex.Message)
_valid = False
End Try
Return _valid
End Using
End Function

Private Sub ValidationHandler(ByVal sender As Object, ByVal vargs As ValidationEventArgs)
If vargs.Severity = XmlSeverityType.Error Then
_valid = False
End If
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message)
End Sub
End Class



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
.



Relevant Pages

  • Re: Automated WSF argument validation: demo script.
    ... The "right" kind of tool for this job is a COM-accessible XML stream reader ... The script is a demo of automating argument validation ... >> end sub ...
    (microsoft.public.windows.server.scripting)
  • Re: Automated WSF argument validation: demo script.
    ... The "right" kind of tool for this job is a COM-accessible XML stream reader ... The script is a demo of automating argument validation ... >> end sub ...
    (microsoft.public.scripting.wsh)
  • Re: 2 Questions about validations
    ... > In SGML and XML, validity means that a document conforms to the Document ... Validation is a process ...
    (comp.infosystems.www.authoring.html)
  • Re: XML Schema Validation + Deserialization
    ... Why not use the validating reader to ... > I would like to perform a 2-pass XML reading from a stream. ... > ' now the validation read - XmlSchemaException exceptions ... > ' actually I want to deserialize the XML into the ...
    (microsoft.public.dotnet.xml)
  • Re: A note on personal corruption as a result of using C
    ... impossible to write effective string validation routines by definition ... Turing machine can be implemented in C, then any validation procedure ... in Haskell or one of the ML family of languages. ... I haven't worked in Haskell or ML, ...
    (comp.programming)