使用 .NET 中的相对路径通过多个 XSD 验证 XML

发布于 2024-08-08 17:52:02 字数 1139 浏览 3 评论 0原文

我正在尝试编写一个通用的 VB.NET (VS2005) 函数来根据 XSD 验证 XML 文档。这工作正常,直到我使用带有相对路径的 XSD,包括:

<xs:include schemaLocation="test.02.xsd" />

它似乎永远找不到任何辅助文档中包含的内容。下面是我原来的功能。我一直在使用 XmlUrlResolver() 但我似乎也无法使用它取得任何进展。任何帮助将不胜感激。

Private Sub ValidatingProcess(ByVal XSDPath As String, ByVal XMLPath As String)
    Try
        Me.Reader = New XmlTextReader(XMLPath)
        Dim SR As New StreamReader(XSDPath)
        Dim Schema As New XmlSchema()
        Schema = XmlSchema.Read(SR, New ValidationEventHandler(AddressOf ReaderSettings_ValidationEventHandler))
        Dim ReaderSettings As New XmlReaderSettings()
        ReaderSettings.ValidationType = ValidationType.Schema
        ReaderSettings.Schemas.Add(Schema)
        AddHandler ReaderSettings.ValidationEventHandler, AddressOf ReaderSettings_ValidationEventHandler
        Dim objXmlReader As XmlReader = XmlReader.Create(Reader, ReaderSettings)
        While objXmlReader.Read()
        End While
    Catch AccessEx As UnauthorizedAccessException
        Throw AccessEx
    Catch Ex As Exception
        Throw Ex
    End Try
End Sub

I'm trying to write a generic VB.NET (VS2005) function to validate an XML document against an XSD. This works fine until I use an XSD with a relative path include like:

<xs:include schemaLocation="test.02.xsd" />

It can never seem to find what is included in any secondary documents. Here is my original function below. I've been playing with XmlUrlResolver() but I cannot seem to make any progress using that either. Any help here would be greatly appreciated.

Private Sub ValidatingProcess(ByVal XSDPath As String, ByVal XMLPath As String)
    Try
        Me.Reader = New XmlTextReader(XMLPath)
        Dim SR As New StreamReader(XSDPath)
        Dim Schema As New XmlSchema()
        Schema = XmlSchema.Read(SR, New ValidationEventHandler(AddressOf ReaderSettings_ValidationEventHandler))
        Dim ReaderSettings As New XmlReaderSettings()
        ReaderSettings.ValidationType = ValidationType.Schema
        ReaderSettings.Schemas.Add(Schema)
        AddHandler ReaderSettings.ValidationEventHandler, AddressOf ReaderSettings_ValidationEventHandler
        Dim objXmlReader As XmlReader = XmlReader.Create(Reader, ReaderSettings)
        While objXmlReader.Read()
        End While
    Catch AccessEx As UnauthorizedAccessException
        Throw AccessEx
    Catch Ex As Exception
        Throw Ex
    End Try
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-08-15 17:52:02

我通过创建一个自定义 XmlUrlResolver 解决了这个问题,如下所示:

Class CustomResolver
    Inherits XmlUrlResolver

    Private _CustomBaseUri As Uri

    Public Sub New(ByVal baseUri As Uri)
        If baseUri.IsFile Then
            _CustomBaseUri = New Uri(Path.GetDirectoryName(baseUri.LocalPath.ToString()) & "\")
        Else
        End If
        Me._CustomBaseUri = baseUri
    End Sub

    Public Overloads Overrides Function ResolveUri(ByVal baseUri As Uri, ByVal relativeUri As String) As Uri
        If baseUri IsNot Nothing Then
            Return MyBase.ResolveUri(baseUri, relativeUri)
        Else
            Return MyBase.ResolveUri(_CustomBaseUri, relativeUri)
        End If
    End Function
End Class

I solved it by creating a custom XmlUrlResolver like this:

Class CustomResolver
    Inherits XmlUrlResolver

    Private _CustomBaseUri As Uri

    Public Sub New(ByVal baseUri As Uri)
        If baseUri.IsFile Then
            _CustomBaseUri = New Uri(Path.GetDirectoryName(baseUri.LocalPath.ToString()) & "\")
        Else
        End If
        Me._CustomBaseUri = baseUri
    End Sub

    Public Overloads Overrides Function ResolveUri(ByVal baseUri As Uri, ByVal relativeUri As String) As Uri
        If baseUri IsNot Nothing Then
            Return MyBase.ResolveUri(baseUri, relativeUri)
        Else
            Return MyBase.ResolveUri(_CustomBaseUri, relativeUri)
        End If
    End Function
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文