vb.net - 如何根据 XML 架构文件验证 XML 字符串

标签 vb.net validation xsd

我正在使用 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/

相关文章:

c# - XMlSerialization 没有序列化 Datetime

javascript - 在更新面板回发中注册ClientScriptBlock

vb.net - 无法从代码隐藏访问我的类(class)。类是App_Code文件夹

Laravel 4 中的验证

c# - 当针对 XML 验证 XSD 时出现错误?

XML 数据绑定(bind)向导备选方案

.net - ASP.NET 中文本框的自动完成搜索组件

wpf - 有没有一种简单的方法可以绑定(bind)到 WPF 中的父行?

c# - 对 ASP.NET Core 中缺少必需属性的响应

Spring 类级别验证和 Thymeleaf