Title Image

Don Xml's Grok This

The home of Don Demsak
Welcome to Don Xml's Grok This Sign in | Help
in Search

This Blog

Syndication

Site Sponsors

DonXml's All Things Techie

Schema Validation of a XmlDocument In .Net 1.1

Validating a XmlDocument using a schema in .Net 1.1 is not as easy as it should be (but it is fixed in System.Xml 2.0).  XmlDocuments are really meant for updating Xml using the Document Object Model, but for some reason WSE and ASMX likes to return XmlDocuments, even though you are more likely to be reading the XML more then updating.  If you want to validate an XML stream that you received thru a web service call, getting an XmlDocument instead of a XmlReader is a bit of a pain in .Net.  At TechEd I had someone come up with a this issue, so I created the following ValidatingXmlDocument class based on System.Xml.XmlDocument.  It is sort of like the new XmlDocument in System.Xml 2.0, but not as full featured.  Since the person that had this problem was using VB.Net, I wrote this in their preferred programming language.

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

Public Class ValidatingXmlDocument
 Inherits XmlDocument

#Region "Private Fields"
 Dim _Schemas As Schema.XmlSchemaCollection
 Dim _IsValidDocument As Boolean = False
#End Region

#Region "Constructors"

Public Sub New()
  MyBase.New()
 End Sub

 Public Sub New(ByVal doc As XmlDocument)
  MyBase.New()
  Dim TempMemoryStream As New MemoryStream
  doc.Save(TempMemoryStream)

  TempMemoryStream.Position = 0
  MyBase.Load(TempMemoryStream)
  _Schemas = New Schema.XmlSchemaCollection
 End Sub


#End Region

 Public Property Schemas() As Schema.XmlSchemaCollection
  Get
   Return _Schemas
  End Get
  Set(ByVal Value As Schema.XmlSchemaCollection)
   _Schemas = Value
  End Set
 End Property

 Public ReadOnly Property IsValidDocument() As Boolean
  Get
   Return _IsValidDocument
  End Get
 End Property

 Public Event ValidationEventHandler As ValidationEventHandler


 Public Sub Validate()
  If ((Me.Schemas Is Nothing) OrElse (Me.Schemas.Count = 0)) Then
   Throw New InvalidOperationException
  End If

  Try
   Dim TempMemoryStream As New MemoryStream
   Me.Save(TempMemoryStream)
   TempMemoryStream.Position = 0
   Dim Reader As New XmlTextReader(TempMemoryStream)


   Dim validator As New XmlValidatingReader(Reader)
   AddHandler validator.ValidationEventHandler, AddressOf Validator_ValidationEventHandler
   validator.Schemas.Add(_Schemas)
   _IsValidDocument = True

   While validator.Read()
   End While

  Catch ex As Exception
   Console.WriteLine(ex.Message)
  End Try

 End Sub

 Private Sub Validator_ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
  _IsValidDocument = False
  RaiseEvent ValidationEventHandler(Me, args)
 End Sub 'ValidationEventHandle

End Class

Published Wednesday, June 15, 2005 10:26 PM by donxml
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About donxml

I’m an independent consultant, specializing in .Net solutions architecture, based out of New Jersey who also doubles as an evangelist for XML, Domain Driven Design, enterprise architecture and .Net. I do not work for Microsoft, the W3C or any other big company that you may know of (at least not yet). I’ve been an indie for over ten years, and although I’ve been tempted a couple times to take a job with companies like Microsoft, I’ve haven’t found something better than my current situation. I work mostly with the large pharmaceuticals that are based here in New Jersey, and usually find myself on long term contracts. Definitely not the prototypical indie consultant, but it lets me dedicate time to my non-income generating activities like the developer community stuff, plus financing open source projects like XPathmania and MVP-XML. If you would like to talk to me about doing some contract work, just contact me via the contact page. My rates vary widely, depending on lots of different variables, but mostly distance from Jersey, and type of work. Plus, I’ve been known to donate some of my code for various projects.
Powered by Community Server, by Telligent Systems