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>
''' <param name="SchemaNamespace"></param>
''' <param name="SchemaStream"></param>
''' <param name="XmlStream"></param>
''' <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(mstrValidateXmlFileErrorMessa ge))
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
"rowe_newsgroups" wrote:
On Jun 29, 9:09 pm, "Andy B" <a_bo...@sbcglobal.netwrote:
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.
Replies to this in your other thread:
http://groups.google.com/group/micro...1b61b8b513dfbc
Thanks,
Seth Rowe [MVP]
http://sethrowe.blogspot.com/