我正在使用Visual Studio 2008在.NET3.5中开发VB Web应用程序。
在将一些XML添加到HTML表单发布到第三方之前,我很难将某些XML验证为字符串。我有来自第三方的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
由于
答案 0 :(得分:3)
这是一个例子: 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
答案 1 :(得分:1)
这就是我最终的目标
Public validationErrors As String = ""
Public Function ValidPortalRequest(ByVal XMLPortalRequest As String) As Boolean
Try
Dim objSchemasColl As New System.Xml.Schema.XmlSchemaSet
objSchemasColl.Add("xxx", "xxx")
objSchemasColl.Add("xxx", "xxxd")
Dim xmlDocument As New XmlDocument
xmlDocument.LoadXml(XMLPortalRequest)
xmlDocument.Schemas.Add(objSchemasColl)
xmlDocument.Validate(AddressOf ValidationEventHandler)
If validationErrors = "" Then
Return True
Else
Return False
End If
Catch ex As Exception
Throw
End Try
End Function
Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)
validationErrors += e.Message & "<br />"
End Sub
与Jose相同,除了我已经将2个XSD添加为SchemaSet而不是使用XMLReader读取它们。