Here is what I'm trying to do:
1.) I have an application that is able to produce an xml document, and xsd and zips the two files together.
2.) Using remoting I send the contents of the zip file to a server application.
3.) My server application uncompresses the information and now has the xml and xsd file.
My question is how do I now use the xsd to validate the xml. Both files are in memory.
Also: I understand it doesn't make sense to send an xsd with every xml document, but for my purposes it does... don't ask....
I'm not even sure if this is the right area to post this question, if not I'm sorry.
This should help you:
XML UI: from XSD to XML
Are both files in memory, or do you still have them physically? if the latter, I have this code:
-------------------------------------------------------------------------
Imports System.Xml
Imports System.Xml.Schema
Public Class XMLValidator
Public Sub New()
End Sub
Public Function Validate(ByVal fileName As String) As Boolean
If SchemaCollection Is Nothing Then
Throw New Exception("Property 'Schema' not set")
End If
Dim ValidatingReader As XmlValidatingReader
Try
ValidatingReader = New XmlValidatingReader(New XmlTextReader(fileName))
ValidatingReader.Schemas.Add(SchemaCollection)
ValidatingReader.ValidationType = ValidationType.Schema
' Set the validation event handler
Dim valdel As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEvent)
AddHandler ValidatingReader.ValidationEventHandler, valdel
' Read XML data
While ValidatingReader.Read()
End While
Finally
If Not ValidatingReader Is Nothing Then
ValidatingReader.Close()
End If
End Try
Return Message Is Nothing
End Function
Public Sub ValidationEvent(ByVal errorid As Object, ByVal args As ValidationEventArgs)
' Missing schema
If (args.Severity = XmlSeverityType.Warning) Then
Message = Message + "No schema found to enforce validation."
Message = Message + vbCrLf
' Not a well formed XML
ElseIf (args.Severity = XmlSeverityType.Error) Then
Message = Message + args.Message
Message = Message + vbCrLf
End If
' XSD schema validation error
If Not (args.Exception Is Nothing) Then
' Message = Message + args.Exception.ToString()
Message = Message + "My guess. Line#: " + CType(args.Exception.LineNumber - 2, String)
Message = Message + vbCrLf
End If
MessageCount += 1
End Sub
Public ReadOnly Property ErrorMessage() As String
Get
Return Message
End Get
End Property
Public WriteOnly Property Schema() As String
Set(ByVal Value As String)
Try
SchemaCollection = New XmlSchemaCollection
Dim Reader As New XmlTextReader(Value)
If SchemaCollection.Add(Nothing, Reader) Is Nothing Then
Throw New Exception("Schema file not loaded")
End If
Reader.Close()
Catch ex As Exception
SchemaCollection = Nothing
Throw ex
End Try
End Set
End Property
Public ReadOnly Property Errors() As Integer
Get
Return Me.MessageCount
End Get
End Property
Private SchemaCollection As XmlSchemaCollection
Private Message As String = Nothing
Private MessageCount As Integer = 0
End Class
---------------------------------------------------------------------------
If not, you can try messing with the IO streams to make them direct memory strings loaded into streams.
thank you
No problem. Let me know how much you had to adapt that!
I already have something simliar that uses file paths so between all the references I have, I'm sure I'll come up with something.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks