我正在尝试找到以下函数的替代方法,该函数使用过时的 System.xml.XmlValidatingReader()。如何调整此代码以使用 XMLReader,这是 Visual Studio 建议的替代方法?随时用 C# 或 VB.NET 发表回复。我传递给函数的 xmlString 是引用 XSD 模式的原始 xml。
'VALIDATE XML
Public Shared Function validateXml(ByVal xmlString As String) As XmlDocument
xmlErrorStr = ""
Dim strReader As New StringReader(xmlString)
Dim xmlr As New XmlTextReader(strReader)
Dim xmlvread As New XmlValidatingReader(xmlr)
' Set the validation event handler
AddHandler xmlvread.ValidationEventHandler, AddressOf ValidationCallBack
Try
While (xmlvread.Read)
End While
Catch ex As Exception
xmlErrorStr = "<Error>" & vbCrLf & "<Exception>The XML document is invalid or malformed</Exception>" & vbCrLf & "</Error>"
End Try
'Close the reader.
xmlvread.Close()
strReader.Close()
Dim xmlDocument As New XmlDocument
xmlDocument.LoadXml("<Errors>" & xmlErrorStr & "</Errors>")
Return xmlDocument
End Function