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
XmlValidatingRe ader, 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 XmlValidatingRe ader 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().Valida te)
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"
elementFormDefa ult="qualified" >
<xsd:element name="Test" default="On">
<xsd:simpleType >
<xsd:restrictio n base="xsd:strin g">
<xsd:enumeratio n value="On" />
<xsd:enumeratio n value="Off" />
</xsd:restriction >
</xsd:simpleType>
</xsd:element>
</xsd:schema>
and here's my XML
<root><Test>O n</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 ValidationHandl er(ByVal Sender As Object, _
ByVal args As System.Xml.Sche ma.ValidationEv entArgs)
m_Error = True
MsgBox(args.Mes sage)
End Sub
Public Function Validate() As Boolean
Dim XsdString As String, XmlString As String
XsdString = "<?xml version=""1.0"" encoding=""UTF-8"" ?>" & _
"<xsd:schem a xmlns:xsd=""htt p://www.w3.org/2001/XMLSchema""
elementFormDefa ult=""qualified "">" & vbCrLf & _
vbTab & "<xsd:eleme nt name=""Test"" default=""On""> " & vbCrLf
& _
vbTab & vbTab & "<xsd:simpleTyp e>" & vbCrLf & _
vbTab & vbTab & vbTab & "<xsd:restricti on
base=""xsd:stri ng"">" & vbCrLf & _
vbTab & vbTab & vbTab & vbTab & "<xsd:enumerati on value=""On""
/>" & vbCrLf & _
vbTab & vbTab & vbTab & vbTab & "<xsd:enumerati on
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.XmlT extReader(New
System.IO.Strin gReader(XsdStri ng))
Dim SchemaStep2 As New System.Xml.Sche ma.XmlSchema
SchemaStep2.Nam espaces.Add("xs d",
"http://www.w3.org/2001/XMLSchema")
SchemaStep2.Com pile(AddressOf ValidationHandl er)
SchemaStep2.Rea d(SchemaStep1, AddressOf ValidationHandl er)
' *** 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.Memor yStream
' 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(Le n(XmlString))
For Index = 0 To XmlByteArray.Ge tUpperBound(0) - 1
XmlByteArray(In dex) = Asc(Mid(XmlStri ng, Index + 1, 1))
Next Index
XmlStream.Write (XmlByteArray, 0, Len(XmlString) - 1)
XmlStream.Posit ion = 0
Dim XmlReader As New System.Xml.XmlT extReader(XmlSt ream) ' at
last!
' *** validate the XML against the schema
Dim Vr As New System.Xml.XmlV alidatingReader (XmlReader)
Vr.ValidationTy pe = Xml.ValidationT ype.Schema
Vr.Schemas.Add( SchemaStep2)
AddHandler Vr.ValidationEv entHandler, AddressOf
ValidationHandl er
Try
While Vr.Read() And Not m_Error
End While
Catch e As Exception
MsgBox(e.Messag e)
m_Error = True
End Try
Validate = Not m_Error
End Function
End Class