473,499 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validate dynamic XML against dynamic XSD using VB.NET

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
1 9081
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1732
by: QuantDev | last post by:
Hi NG, I would need to validate an XML fragment against a type defined within an XSD (which defines many other things). What is the correct way of achieving this? QD2004
1
1428
by: Ron Rohrssen | last post by:
I've been working on learning XML schemas and trying to make use of the MS classes for validating data against a schema. So, I've been trying to work through some simple schemas and instances....
2
5021
by: IanT | last post by:
Hi I need help with my approach to validating de-serialized XML received via webservice. Hopefully someone can point me to sources detailing best practice for the following. I agreed a...
6
4416
by: LesleyW | last post by:
Hi Apologies if this is a really dumb question, but being new to XML and Schemas, I wonder if giving the namespace for eg xsd or xsi as a website address means that the user has to be online...
7
16212
by: Ali-R | last post by:
Hi all, I am getting a CSV file like this from our client: "C1","2","12344","Mr","John","Chan","05/07/1976"......... I need to validate **each filed value** against a set of rules ,for...
2
1763
by: John H | last post by:
Hi, How can i just use the XmlDocument object to validate an xml instanace against a schema referenced inside the xml instance? The Load method seems to not validate it against the schema. ...
3
1838
by: Rushi | last post by:
Hi All, Is it possible to Validate XML against XSD using two different XML files. scenario: There are two different XML file, one file contain element and count information and other one...
2
3010
by: Reid Priedhorsky | last post by:
Hi folks, I have a need to validate XML files against both DTDs and XML Schema from the command line. In an ideal world, I'd be able to do something like: $ python validate.py foo.xml ...
1
1572
by: JoeZ | last post by:
Hi all, I am using XMLValidatingReader to validate an xml instance against the xml schema. Currently the schema has the targetnamespace, but the instance doesn't have a namespace. Can I still...
0
7223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7390
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5475
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3103
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.