Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Xml
Imports System.Xml.Schema
Imports Microsoft.SqlServer.Server
Public Class YourFunkyClassName
' Validates XML against XSD - returns schema validation error if found
Public Shared Sub ValidateXML(ByVal XMLtoValidate As String, ByVal XSD As String, ByRef FailureMessage As String)
Dim sqlP As SqlPipe = SqlContext.Pipe()
Dim schemaError As String = ""
Try
' Check that the XMLtoValidate is well formed
Dim IsValidXML As New XmlDocument
Try
IsValidXML.LoadXml(XMLtoValidate)
Catch ex As Exception
FailureMessage = "XML was not well formed"
'Dim sdr As New SqlDataRecord(New SqlMetaData("FailureMessageAsResult", SqlDbType.NVarChar, 4000))
'sqlP.SendResultsStart(sdr)
'sdr.SetString(0, FailureMessage)
'sqlP.SendResultsRow(sdr)
'sqlP.SendResultsEnd()
Exit Sub
End Try
Dim SchemaReader As XmlReader = XmlReader.Create(New StringReader(XSD))
' Load the schema into the schema object...
Dim Schema As XmlSchema = XmlSchema.Read(SchemaReader, Nothing)
' Check that namespace from the schema is the same as the XML to validate
Dim ns As String = ""
If Not IsValidXML.FirstChild.Attributes("xmlns") Is Nothing Then
ns = IsValidXML.FirstChild.Attributes("xmlns").InnerText
End If
If Schema.TargetNamespace <> ns Then
FailureMessage = "XML has an invalid namespace"
'Dim sdr As New SqlDataRecord(New SqlMetaData("FailureMessageAsResult", SqlDbType.NVarChar, 4000))
'sqlP.SendResultsStart(sdr)
'sdr.SetString(0, FailureMessage)
'sqlP.SendResultsRow(sdr)
'sqlP.SendResultsEnd()
Exit Sub
End If
' Configure the reader to use validation, and add the schema we just loaded...
Dim SchemaReaderSettings As New XmlReaderSettings()
SchemaReaderSettings.ValidationType = ValidationType.Schema
SchemaReaderSettings.Schemas.Add(Schema)
SchemaReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
SchemaReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
SchemaReaderSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation
' Read the XML into an XmlReader...
Dim XMLtoValidateReader As XmlReader = XmlReader.Create(New StringReader(XMLtoValidate))
' Create a reader that will read the document and validate it against the XSD...
Dim ValidationReader As XmlReader = XmlReader.Create(XMLtoValidateReader, SchemaReaderSettings)
' Validate the document...
Do While ValidationReader.Read()
Loop
ValidationReader.Close()
Catch exXml As XmlException
schemaError = "Error on line " & exXml.LineNumber & ", "
schemaError += "Position " & exXml.LinePosition & ", "
schemaError += exXml.Message
FailureMessage = schemaError
Catch exXsd As XmlSchemaException
schemaError = "Error on line " & exXsd.LineNumber & ", "
schemaError += "Position " & exXsd.LinePosition & ", "
schemaError += exXsd.Message
FailureMessage = schemaError
Catch ex As Exception
sqlP.Send("Schema validation exception - " & ex.Message.ToString)
FailureMessage = schemaError
End Try
End Sub
End Class