0
  1. 我收到错误

    文档上的 XmlSchemaSet 为 null 或其中没有架构。在调用 Validate 之前提供架构信息。

    但是我的架构文件位于指定的位置:C:\InvSchema.xsd

    没有传递特定的命名空间,所以我将其设置为空。

    那么,为什么我会收到此错误?

  2. 我还需要知道 xml 模式是否已成功验证,但 validate 函数不返回确认它的布尔值。

    我如何从中获得该值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
4

1 回答 1

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

您还应该在事件处理程序中设置成员级别的布尔值 (_valid)。每次发现的模式违规都会调用事件处理程序。

于 2013-09-12T21:03:23.747 回答