.NET Framework - validating if a file is a true valid xml file that fits a certain schema

Asked By Andy B on 29-Jun-08 09:09 PM
I need to make sure that a file saved in a particular place is a valid xml
file that fits a certain schema. Where would I get started doing this? The
original file would have been created and saved with a dataset.


Cor Ligthert[MVP] replied on 29-Jun-08 11:33 PM
Andy,

Just set it in a Try and Catch block with a dataset.ReadXML

Cor
rowe_newsgroups replied on 01-Jul-08 06:57 AM
l
e

Replies to this in your other thread:

http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_=
thread/thread/cf1b61b8b513dfbc

Thanks,

Seth Rowe [MVP]
http://sethrowe.blogspot.com/
surtur replied on 30-Jun-08 10:23 PM
It's pretty fiddly, here's my code:

''' <summary>
''' Validates an XML stream against the specified XML Schema. Raises
ValidateXmlFileException if the XML stream or XML Schema fails validation.
''' </summary>
''' 
''' 
''' 
''' <exception cref="ValidateXMLFileException">Raised if XML or XML
File fails validation</exception>
''' <remarks></remarks>
Public Sub ValidateXmlFile(ByVal SchemaNamespace As String, ByVal
SchemaStream As Stream, ByVal XmlStream As Stream)

' Create the XmlSchemaSet class.
Dim sc As XmlSchemaSet = New XmlSchemaSet()

Try
' Add the schema to the collection.
Dim xrInput As New XmlTextReader(SchemaStream)
sc.Add(SchemaNamespace, xrInput)

' Set the validation settings.
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.Schemas = sc
AddHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
' Create the XmlReader object.
Using reader As XmlReader = XmlReader.Create(XmlStream,
settings)
' Parse the file.
Do While reader.Read() : Loop
End Using
If mstrValidateXmlFileErrorMessage > "" Then
Throw New ValidateXMLFileException("The XML Stream
failed validation", New ApplicationException(mstrValidateXmlFileErrorMessage))
End If
RemoveHandler settings.ValidationEventHandler, AddressOf
ValidateXmlFile_Callback
Catch ex As XmlSchemaException
Throw New ValidateXMLFileException("Could not parse schema",
ex)
Catch ex As XmlException
Throw New ValidateXMLFileException("Could not parse XML", ex)
End Try

End Sub
Private Sub ValidateXmlFile_Callback(ByVal sender As Object, ByVal e
As ValidationEventArgs)
mstrValidateXmlFileErrorMessage = e.Message
End Sub
Public Class ValidateXMLFileException
Inherits Exception
Sub New(ByVal ErrorMessage As String, ByVal InnerException As
Exception)
MyBase.New(ErrorMessage, InnerException)
End Sub
End Class

--
David Streeter
Synchrotech Software
Sydney Australia
Cor Ligthert[MVP] replied on 30-Jun-08 11:35 PM
SuturZ,

Can you explain what it does more then my simple line of code in a Try and
Catch?

Cor
surtur replied on 01-Jul-08 01:10 AM
My code tests against a supplied XML Schema.

I don't know if Dataset.ReadXML does that. I've never used Dataset.ReadXML
though, perhaps it is a better approach.

BTW I think in my original post I missed a module level String variable
declaration:

Private mstrValidateXmlFileErrorMessage As String 'used by ValidateXmlFile()

--
David Streeter
Synchrotech Software
Sydney Australia
surtur replied on 01-Jul-08 10:01 PM
I just had a play around with the Dataset XML functions. They are VERY good,
and are the better solution for the OP.

However, they seem to work only for XML/XSDs that have been generated from a
Dataset. If you are sourcing your XML/XSD from a different source, then my
code might be necessary (it can validate any XML file against any schema).

--
David Streeter
Synchrotech Software
Sydney Australia