我正在尝试针对模式验证 xml。我正在使用 XmlReaderSetting 并尝试遵循 MSDN 上的示例,但无法使其工作。即使我针对模式抛出一个完全不同的文件,它也不会验证 xml。谁能解释我错过了什么?
谢谢,
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