Reputation: 7823
I am trying to validate xml against a schema. I am using XmlReaderSetting and trying to follow an example on MSDN but not able to make it work. It doesn't validate the xml even I throw a totally different file against schema. Can anyone explain me what I am missing?
Thanks,
Protected Sub ValidateXML(xmlFilePath As String, schemasFilePath As String)
Try
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", schemasFilePath)
settings.ValidationType = ValidationType.Schema
Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
Dim document As XmlDocument = New XmlDocument()
document.Load(reader)
Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
' the following call to Validate succeeds.
document.Validate(eventHandler)
reader.Close()
Catch ex As Exception
Messagebox(ex.Message, "error")
End Try
End Sub
Protected Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
'Messagebox(e, "error")
Case XmlSeverityType.Warning
'Messagebox(e, "error")
End Select
End Sub
Upvotes: 2
Views: 10279
Reputation: 1
It is not calling the Event Handler:
Protected Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
'Messagebox(e, "error")
Case XmlSeverityType.Warning
'Messagebox(e, "error")
End Select
End Sub
Upvotes: 0
Reputation: 43743
You are mixing two different ways to read the XML file. You are using an XmlReader
object and an XmlDocument
object. Typically, you'd only use one or the other. It will work to use both, as you have done, but it does introduce some unnecessary confusion.
The reason the validation is not working is because you are adding the schema validation to the reader, but then you attach the ValidationEventHandler
method to the XmlDocument
object. Both XmlDocument
and XmlReader
are capable of performing schema validation, and they each have their own XmlSchemaSet
and validation event handler that they use to perform the the validation. You have given half of what they need to each of them instead of all of what they need to one or the other. In other words, you have done the following:
As such, neither object has all the information it needs to properly validate. The XmlReader
object will be performing the validation, but you won't be notified of any of the errors that it finds, whereas the XmlDocument
object will not be doing any validation at all, but does have the capability to notify you, in the event that it did find any validation errors. To fix it, you need to either set the XmlReader
object's validation event handler, or you need to set the XmlDocument
object's schema. For instance:
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", schemasFilePath)
settings.ValidationType = ValidationType.Schema
AddHandler settings.ValidationEventHandler, New ValidationEventHandler(AddressOf ValidationEventHandler)
Dim reader As XmlReader = XmlReader.Create(xmlFilePath, settings)
' Read the document...
Upvotes: 3