Google
WWW Yariv Hammer's Code Site

Wednesday, January 04, 2006

Loading and Validating Xml Files

Source code in VB (with demo) can be downloaded from here (~35 KB)

Introduction
An Xml should be Well-Formed:
- must begin with the XML declaration
- must have one unique root element
- all start tags must match end-tags
- XML tags are case sensitive
- all elements must be closed
- all elements must be properly nested
- all attribute values must be quoted
- XML entities must be used for special characters

However, being Well-Formed is not enough. There can still be errors in the Xml. Those errors are in the logical aspects, rather than the syntax. Xml Schemas help us define the business rules of our Xmls. Xml Schemas are written in Xml and they support Data Types.

It is a good practice to use Xml Schemas always. Don't write just Xmls. Use schemas as well!!!

Xml and Xml Schemas In .NET
As you probably know there is a wide support for Xml in the .NET . The class XmlDocument is the DOM implementation in the .NET Framework. This is a very strong class. However, there is no easy support for loading and validating the Xml against its Schema.

Validating Against the Schema
My friend, Dror Givoli, who works with me, supplied a code to a class, ValidatingXmlDocument, derived from XmlDocument, which has a method called LoadAndValidate, with tons of overloads. Here are few:
-----------------------------------------------------------
Public Overloads Sub LoadAndValidate(ByVal filename As String, ByVal Schema As XmlSchema)
MyBase.Load(filename)
Validate(Schema)
End Sub
Public Overloads Sub LoadAndValidate(ByVal inStream As Stream, ByVal Schema As XmlSchema)
MyBase.Load(inStream)
Validate(Schema)
End Sub
Public Overloads Sub LoadAndValidate(ByVal txtReader As TextReader, ByVal Schema As XmlSchema)
MyBase.Load(txtReader)
Validate(Schema)
End Sub
Public Overloads Sub LoadAndValidate(ByVal reader As XmlReader, ByVal Schema As XmlSchema)
MyBase.Load(reader)
Validate(Schema)
End Sub
Public Sub LoadXmlAndValidate(ByVal xml As String, ByVal Schema As XmlSchema)
MyBase.LoadXml(xml)
Validate(Schema)
End Sub
Public Overloads Sub LoadAndValidate(ByVal filename As String, ByVal schemaFileName As String)
Dim Schema As XmlSchema = XmlSchema.Read(New XmlTextReader(schemaFileName), AddressOf ValidationEventHandler)
MyBase.Load(filename)
Validate(Schema)
End Sub

----------------------------------------------------------
There are more. The Validate method is as follows:
----------------------------------------------------
Public Sub Validate(ByVal Schema As XmlSchema)
Dim ms As New MemoryStream
Me.Save(ms)
ms.Position = 0
Dim rdr As New XmlTextReader(ms)
Dim vrdr As New XmlValidatingReader(rdr)
vrdr.Schemas.Add(Schema)
AddHandler vrdr.ValidationEventHandler, AddressOf ValidationEventHandler
While vrdr.Read
End While
End Sub


Private Sub ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
Throw New XmlValidationException(args)
End Sub

--------------------------------------------------
Last there is an implementations of the method SaveAndValidate, again with tons of overloads:
--------------------------------------------------
Public Overloads Sub SaveAndValidate(ByVal filename As String, ByVal Schema As XmlSchema)
Validate(Schema)
Save(filename)
End Sub
Public Overloads Sub SaveAndValidate(ByVal outStream As System.IO.Stream, ByVal Schema As XmlSchema)
Validate(Schema)
Save(outStream)
End Sub
Public Overloads Sub SaveAndValidate(ByVal writer As System.IO.TextWriter, ByVal Schema As XmlSchema)
Validate(Schema)
Save(writer)
End Sub

-----------------------------------------
And there are more overloads.

Demo
Here is an example of how to use this class:
------------------------------------------
Dim vDoc As New ValidatingXmlDocument
Dim xmlFile As String = "c:\MyXmlFile.xml"
Dim schemaFile As String = "c:\MySchemaFile.xsd"
Try
vDoc.LoadAndValidate(xmlFile, schemaFile)
Catch ex As XmlValidationException
Console.Out.WriteLine(ex.Args.Message)
End Try

------------------------------------------

Acknowledgment
This code was given to me by Dror Givoli, in order to post it in my blog. Thanks Dror for sharing your knowledge with others.

0 Comments:

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.