我正在使用 Visual Studio 2008 在 .NET3.5 中开发 VB Web 应用程序。
在将某些 XML 添加到 HTML 表单以发布到第三方之前,我很难将其验证为字符串。我有一个来自第 3 方的 XML 架构文件可供验证,此时我希望应用程序在每次发布之前执行验证。
搜索后,我发现了对 XmlValidatingReader 的引用,但这已经过时了,我很难找到另一种方法来实现它。
此外,所有好的示例都是用 C# 编写的 - 现在我只能使用 VB。这就是我到目前为止所拥有的,我正在寻求帮助!
Public Function ValidateXML(ByVal strXML As String) As Boolean
' er how do I get the schema file into here?
Dim schema As XmlReader
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("", schema)
settings.ValidationType = ValidationType.Schema
' When I use LoadXML to get the string I can't use the settings object above to get the schema in??
Dim document As XmlDocument = New XmlDocument()
document.LoadXml(strXML)
document.Validate(AddressOf ValidationEventHandler)
End Function
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
' Gonna return false here but haven't got to it yet! Prob set a variable for use above
End Sub
谢谢
最佳答案
这是一个例子: XmlSchemaValidator in VB.NET
更新 - 试试这个:
Public Function ValidateXML(ByVal strXML As String) As Boolean
Dim xsdPath As String = "path to your xsd"
Dim schema As XmlReader = XmlReader.Create(xsdPath)
Dim document As XmlDocument = New XmlDocument()
document.LoadXml(strXML)
document.Schemas.Add("", schema)
document.Validate(AddressOf ValidationEventHandler)
End Function
关于vb.net - 如何根据 XML 架构文件验证 XML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/771874/