As part of a project I have a vb program that takes an XML document (payload-XML), which contains a lot of data, but no namespaces. It is run through an XSLT transformation which creates an XML message (Message XML) to be dilivered to another system, then that XML message is validated in code.
The Message XML to the other system does use namespaces and XSD validation, and one of its XML fields is the original XML document, in it's entrity.
The Code-based validator I am using is based of one of the many VB.net 1.1 code examples avilable online - using an XMLValidating reader that has the schema assign to it, reads the xml from an xmlreader and creates a validation event when there is a problem.
The Message XML fields are all typed, and the type of the Payload XML field is xs:anytype <- the payload XML is supposed to be trated like a black box and ignored during XSD validation.
Now After a bit of work it validates most of the message, but fails when it gets to the payload-XML tag. Giving errors that the payload XML tags have not been declared.
However, if I save the XML document to disk, load it in XMLSpy and use that to validate it validates fine.
Sample Message
[HTML]
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<Message xsi:schemaLocation="http://[PrefixURL]/Message C:/[FilePath]/XSD/message.xsd" xmlns="http://[PrefixURL]/Message" xmlns:cmh="http://[PrefixURL]/CMH" xmlns:IT01407="http://[PrefixURL]/IT01407" xmlns:types="http://[PrefixURL]/Types" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cmh:Cmh>
<cmh:InterfaceId>IT01407</cmh:InterfaceId>
<cmh:ServiceName>NOT.USED</cmh:ServiceName>
<cmh:Version>2.0.1</cmh:Version>
<cmh:To Destination="SERVER2" />
<cmh:From SourceAddress="UK003680" ComponentVersion="2.0.1" ComponentId="SERVER5" />
<cmh:Tracking>
<cmh:Item Timestamp="2007-05-02 11:22:82.054" AppInfo="TO.BE.CHANGED" Event="SENDMESSAGE.IT01407" />
</cmh:Tracking>
</cmh:Cmh>
<Body>
<IT01407:IT01407Body>
<IT01407:ClientSurname>DILLON</IT01407:ClientSurname>
<IT01407:ClientForename>THERESA</IT01407:ClientForename>
<IT01407:NfdsApplied>false</IT01407:NfdsApplied>
<IT01407:ReportData>
<payloadXML>
<PayloadXML-Tag-2>There are a lot of tags in payloadXML, none of them have any namespaces</PayloadXML-Tag-2>
</payloadXML>
</IT01407:ReportData>
</IT01407:IT01407Body>
</Body>
</Message>
[/HTML]
As said: <IT01407:ReportData> is of xs:Anytype as we want the tags contents to be treated as a black box. Within it is a large XML document that has no namespaces. If the PayloadXML is removed then the document validates fine in code.
With or without the payload it validates fine in XMLSpy (which is the expected behaviour)
Error returned by the code validator (a list of the event-args.message results):
The 'payloadXML' element is not declared. An error occurred at , (22, 10).(Line,Position)
The 'PayloadXML-Tag-2' element is not declared. An error occurred at , (23, 12).(Line,Position)
....
continuing until it has errored every line in payloadXML
My Guess is I am missing an "ignore everything in this tag" notation somewhere, but am not sure where, XML spy just does it by default, vb doesn't.
Validator VB code:
Expand|Select|Wrap|Line Numbers
- Private _bValid as boolean
- Private _oErrors as StringCollection
- Public Function ValidateXMLAgainstXSD(ByVal oXMLDoc As XmlDocument, ByVal sXSDPath As String) As Boolean
- Dim oStream As MemoryStream
- Dim oValidator As XmlValidatingReader
- Dim oXMLReader As XmlTextReader
- Dim oSchemaReader As XmlTextReader
- Dim oSchema As XmlSchema
- Dim oErrorMessage As Text.StringBuilder
- Dim sError As String
- Try
- _oErrors = New StringCollection
- oSchema = New XmlSchema
- oStream = New MemoryStream
- oSchemaReader = New XmlTextReader(sXSDPath)
- oXMLDoc.Save(oStream)
- oStream.Position = 0
- oXMLReader = New XmlTextReader(oStream)
- oSchema.Read(oSchemaReader, AddressOf Validator_ValidationEventHandler)
- oValidator = New XmlValidatingReader(oXMLReader)
- AddHandler oValidator.ValidationEventHandler, AddressOf Validator_ValidationEventHandler
- oValidator.Schemas.Add(oSchema)
- oValidator.ValidationType = ValidationType.Schema
- _bValid = True
- While oValidator.Read()
- End While
- If Not _bValid Then
- oErrorMessage = New Text.StringBuilder
- oErrorMessage.Append("XML Validation Error: " & Chr(13))
- For Each sError In _oErrors
- oErrorMessage.Append(sError & Chr(13))
- Next
- ErrorLogging.LogError(oErrorMessage.ToString)
- End If
- oSchemaReader.Close()
- Return _bValid
- Catch ex As Exception
- oSchemaReader.Close()
- ErrorLogging.LogError(ex)
- End Try
- End Function
- Private Sub Validator_ValidationEventHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
- _bValid = False
- _oErrors.Add(args.Message & "(Line,Position)")
- End Sub