By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,819 Members | 1,459 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,819 IT Pros & Developers. It's quick & easy.

validate dynamic XML against dynamic XSD using VB.NET

P: n/a
Greetings All

If this is the wrong place to post this question, please give me a push
in the right direction. Thanks.

I know there has to be a simpler way to do this, but this is as simple
a way as I could come up with. Yes, it's obvious I don't know what I'm
doing.

I'm creating an XSD string and an XML string at runtime (ie: NOT read
from file). I want to validate the XML against the XSD. (All the
examples in MSDN and on google seem to read the XML and XSD from file.)

It appears that the XSD is not getting properly loaded or parsed
because after I add the XmlSchema object to the Schemas property of the
XmlValidatingReader, Item(0) of that collection is Nothing (although
the Count of that collection is 1.)

In addition, the Elements collection of the XmlSchema object has a
Count of 0, even after I Read from the XmlTextReader into the
XmlSchema.

Anyway, the XmlValidatingReader chokes on the first element in the XML,
regardless of what it is (ie: regardless of whether the root element is
there or not).

I'm sorry about the code formatting in this message. Google doesn't
seem to want to keep my indentations, regardless of whether I use
spaces or tabs.

To test this class, create a standard Windows project, add the class
module, then put this in your Form1_Load event handler:

MsgBox(New Class1().Validate)

For simplicities sake, here's my schema (also constructed in code):

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:element name="Test" default="On">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="On" />
<xsd:enumeration value="Off" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>

and here's my XML

<root><Test>On</Test></root>

You can also find this project at
http://www.wvi.com/~tevans/RunValidate.zip (21,571 bytes).

Please feel free to point out any superfluous code that I have in here.
From my perspective, this is WAY too long.


Thank you in advance for your assistance.

Tony
Public Class Class1

Private m_Error As Boolean

Public Sub ValidationHandler(ByVal Sender As Object, _
ByVal args As System.Xml.Schema.ValidationEventArgs)
m_Error = True
MsgBox(args.Message)
End Sub

Public Function Validate() As Boolean

Dim XsdString As String, XmlString As String

XsdString = "<?xml version=""1.0"" encoding=""UTF-8"" ?>" & _
"<xsd:schema xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
elementFormDefault=""qualified"">" & vbCrLf & _
vbTab & "<xsd:element name=""Test"" default=""On"">" & vbCrLf
& _
vbTab & vbTab & "<xsd:simpleType>" & vbCrLf & _
vbTab & vbTab & vbTab & "<xsd:restriction
base=""xsd:string"">" & vbCrLf & _
vbTab & vbTab & vbTab & vbTab & "<xsd:enumeration value=""On""
/>" & vbCrLf & _
vbTab & vbTab & vbTab & vbTab & "<xsd:enumeration
value=""Off"" />" & vbCrLf & _
vbTab & vbTab & vbTab & "</xsd:restriction>" & vbCrLf & _
vbTab & vbTab & "</xsd:simpleType>" & vbCrLf & _
vbTab & "</xsd:element>" & vbCrLf & _
"</xsd:schema>"
XmlString = "<Test>Off</Test>"

' *** construct the schema to validate against

Dim SchemaStep1 As New System.Xml.XmlTextReader(New
System.IO.StringReader(XsdString))
Dim SchemaStep2 As New System.Xml.Schema.XmlSchema
SchemaStep2.Namespaces.Add("xsd",
"http://www.w3.org/2001/XMLSchema")
SchemaStep2.Compile(AddressOf ValidationHandler)
SchemaStep2.Read(SchemaStep1, AddressOf ValidationHandler)
' *** construct the XML to validate

' can't write a string to an XmlStream, so have to use a byte
array
Dim XmlByteArray As Byte()

' XmlTextReader won't read from a string, so we have to use a
stream
Dim XmlStream As New System.IO.MemoryStream

' can't assign a string to a byte array, so have to copy it
character by character
' don't know how this is going to work if the schema ends up
being Unicode
Dim Index As Integer
ReDim XmlByteArray(Len(XmlString))
For Index = 0 To XmlByteArray.GetUpperBound(0) - 1
XmlByteArray(Index) = Asc(Mid(XmlString, Index + 1, 1))
Next Index
XmlStream.Write(XmlByteArray, 0, Len(XmlString) - 1)
XmlStream.Position = 0
Dim XmlReader As New System.Xml.XmlTextReader(XmlStream) ' at
last!
' *** validate the XML against the schema
Dim Vr As New System.Xml.XmlValidatingReader(XmlReader)
Vr.ValidationType = Xml.ValidationType.Schema
Vr.Schemas.Add(SchemaStep2)
AddHandler Vr.ValidationEventHandler, AddressOf
ValidationHandler

Try
While Vr.Read() And Not m_Error
End While
Catch e As Exception
MsgBox(e.Message)
m_Error = True
End Try
Validate = Not m_Error
End Function
End Class

Nov 12 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Well, by using an inline XSD schema, I side stepped the entire problem.

It's not as efficient, but boy is the code cleaner.

(I also had some minor problems with my XSD schema -- like I needed an
xmlns="" in the xsd:schema element.)

I have removed the project from the server I had it on.
Please disregard.

Thanks
Tony

Nov 12 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.