将xml作为内存流验证为xsd为字符串

时间:2011-10-07 08:10:49

标签: xml vb.net validation xsd

我想针对 XSD (VB.NET)验证xml。我的功能看起来像这样:

Private Function ValidateXML(ByVal xml As MemoryStream, ByVal xsd As String) As    ArrayList
End Function

当我的xml是一个内存流/流(我使用流,因为我不需要存储xml)和我的xsd是一个字符串(我从Web服务器中获取我的xsd作为字符串)时,如何进行验证? ,我不需要将其存储在文件中)?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

也许这样的事情。

Imports System.Xml
Imports System.IO
Imports System.Xml.Schema

Module Module1

    Private isvalid As Boolean

    Sub Main()
        Dim xml As MemoryStream
        Dim xsd As String
        Dim settings As New XmlReaderSettings()
        settings.ValidationType = ValidationType.Schema
        settings.Schemas.Add("Schema:name", xsd)
        AddHandler settings.ValidationEventHandler, AddressOf MyValidationEventHandler

        Dim v = XmlReader.Create(xml, settings)

        While (v.Read())

        End While

        If isValid Then
            Console.WriteLine("Document is valid")
        Else
            Console.WriteLine("Document is invalid")
        End If
    End Sub



    Public Sub MyValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
        isValid = False
        Console.WriteLine("Validation event\n" + args.Message)
    End Sub
End Module