Top DevX Stories
Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Search the forums:

Go Back   DevX.com Forums > DevX Developer Forums > XML

Reply
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old 12-15-2004, 05:39 PM
coenen coenen is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Vb.net validate xml against xsd and catch all errors

I would like to validate an xml data stream against an external xsd (IE xml stream does not reference it in header) that I have on my local web server in vb.net. I would also like to trap all the errors in validating the document and return them to the user.

Does anyone have some good sample code that would do this. Essentially I would like the functionality of the gotdotnet xsd validator at http://apps.gotdotnet.com/xmltools/...or/Default.aspx

I actually have code that reads an validates, only problem is once it finds one error and throws an exception it won't continue....I would like to know all of the errors in the document to return to the UI.

Here is my code.

---------------------------------------------
Dim objWorkingXML As New System.Xml.XmlDocument
Dim objValidateXML As System.Xml.XmlValidatingReader
Dim objSchemasColl As New System.Xml.Schema.XmlSchemaCollection

objSchemasColl.Add("", Server.MapPath("MYSchema.xsd"))
objValidateXML = New System.Xml.XmlValidatingReader(New System.Xml.XmlTextReader(MYXMLSTREAM))

'This is WHERE the validation occurs.. WHEN the XML Document READS through the validating reader

Try
objWorkingXML.Load(objValidateXML)
Catch ex As Exception
Response.Write(ex.Message.ToString)
Finally
'Close the stream
objValidateXML.Close()
End Try

'The document is valid
Response.Write("THE DOCUMENT IS VALID")
Reply With Quote
  #2  
Old 12-17-2004, 12:57 PM
coenen coenen is offline
Registered User
 
Join Date: Dec 2004
Posts: 2
Here is the correct code

public class Validate

Public Shared mErrorsList As New ArrayList

-----------------------------------------------------------------------------
Shared Function Validate(ByVal Schema As String, ByVal XMLDoc As Stream) As ArrayList

Dim objWorkingXML As New System.Xml.XmlDocument
Dim objValidateXML As System.Xml.XmlValidatingReader
Dim objSchemasColl As New System.Xml.Schema.XmlSchemaCollection
mErrorsList.Clear()

objSchemasColl.Add("", Schema)

'This loads XML string
objValidateXML = New System.Xml.XmlValidatingReader(New System.Xml.XmlTextReader(XMLDoc))

objValidateXML.Schemas.Add(objSchemasColl)

'This is how you CATCH the errors (with a handler function)
AddHandler objValidateXML.ValidationEventHandler, AddressOf ValidationCallBack

'This is WHERE the validation occurs.. WHEN the XML Document READS throughthe validating reader
Try
objWorkingXML.Load(objValidateXML)

Catch ex As Exception
mErrorsList.Add(ex.Message)
Finally

objValidateXML.Close()
End Try


Return mErrorsList

End Function

-----------------------------------------------------------------------------
Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)
mErrorsList.Add(e.Message)
End Sub

End Class
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 12:55 AM.


Sponsored Links



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.