|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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") |
|
#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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|