使用 .NET 中的相对路径通过多个 XSD 验证 XML
我正在尝试编写一个通用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过创建一个自定义 XmlUrlResolver 解决了这个问题,如下所示:
I solved it by creating a custom XmlUrlResolver like this: