我收到错误
文档上的XmlSchemaSet为null或其中没有架构。在调用Validate之前提供架构信息。
但是我的架构文件位于指定的位置:C:\InvSchema.xsd
没有传递特定的命名空间,所以我将其设置为空。
那么,为什么我会收到此错误?
我还需要知道xml架构是否已成功验证,但验证函数没有返回确认它的布尔值。
如何从InvImp.Validate(evtHandler)
获取该值?
以下是代码:
Private Function ValidateSchema() As Boolean
Try
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("", "C:\InvSchema.xsd")
settings.ValidationType = ValidationType.Schema
Dim evtHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)
Dim reader As XmlReader = XmlReader.Create(_fileName, settings)
Dim InvImp As XmlDocument = New XmlDocument
InvImp.Validate(evtHandler)
ValidateSchema = True
Catch ex As Exception
Throw ex
ValidateSchema = False
End Try
End Function
Shared Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
Select Case e.Severity
Case XmlSeverityType.Error
Library.Log("Schema validation failed error " & e.Message, LogType.ERRORLOG, ImportType.InvcIMP)
Case XmlSeverityType.Warning
Library.Log("Schema validation warning " & e.Message, LogType.EVENTLOG, ImportType.InvcIMP)
End Select
End Sub
答案 0 :(得分:0)
我认为有两个问题。事件处理程序没有正确添加,xmlReader应该用于验证而不是单独的XmlDocument对象:
Private Function ValidateSchema() As Boolean
Try
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("", "C:\InvSchema.xsd")
settings.ValidationType = ValidationType.Schema
AddHandler settings.ValidationEventHandler, AddressOf ValidationEventHandler
Dim reader As XmlReader = XmlReader.Create(_fileName, settings)
Do While reader.Read
'Triggers validation'
Loop
Catch ex As Exception
Throw ex
End Try
Return _valid
End Function
您还应该在事件处理程序中设置成员级别boolean(_valid)。将为每个发现的模式违规调用事件处理程序。