It seems like validating an XML document against a schema would be a pretty simple thing to do in the .Net framework, seeing as how they have such deep support for dealing with XML.
But as of yet, I haven't found a simple way to do it.
Here is the code I have been using to validate XML against an XML Schema.
Imports System.IOImports System.XmlImports System.Xml.Schema Private schemaValidation As New ValidationEventHandler(AddressOf ValidationHandler) Private Function ValidateXml(ByVal xmlFileName As String, ByVal xmlSchemaName As String) As Boolean Using myFile As New FileStream(xmlFileName, FileMode.Open, FileAccess.Read, FileShare.None) Dim xDoc As New Xml.XmlDocument() xDoc.Load(myFile) xDoc.Schemas.Add(GetSchema(xmlSchemaName)) Try xDoc.Validate(schemaValidation) Return True Catch ex As XmlSchemaValidationException Console.Write(ex.ToString) Return False Catch ex As XmlSchemaException Console.Write(ex.ToString) Return False Catch ex As Exception Console.Write(ex.ToString) Return False End Try End Using End Function Private Function GetSchema(ByVal filePath As String) As XmlSchema Dim schema As XmlSchema Using s As New System.IO.FileStream(filePath, FileMode.Open) Using reader As New StreamReader(s) schema = XmlSchema.Read(reader, Nothing) End Using End Using Return schema End Function Private Sub ValidationHandler(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs) Throw e.Exception End Sub
Remember Me
Powered by: newtelligence dasBlog 1.9.6264.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
E-mail
Theme design by Jelle Druyts