473,763 Members | 1,908 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
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

Nov 12 '05 #1
1 9107
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
1753
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
1446
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. But, the parser inside of Visual Studio seems to frequently validate against a cached version of the schema. Occassionally I have an XML file that won't validate
2
5052
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 standard data format with my client using an XSD. During development we transferred actual XML files which i validated against the XSD, this was no problem.
6
4434
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 when they run the app? If so, what should I do if I can't guarantee that they will be? Can I bring all these files into the app? Is that desirable? I'm about to write an app that takes an XML file as input, using Visual Studio 2005 Express and SQL...
7
16278
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 instance for "05/07/1976" ,I need to make sure that it's in the right format ,It's not later than today and lots of other rules ,Is there somebody who can help me how to that?Can I map it to some sort of xml schema or something?
2
1784
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. Thanks John
3
1849
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 contain name and other information like color, weight etc..... They are linked by XLink...
2
3029
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 which would then parse and validate foo.xml using the schema or DTD
1
1593
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 use XMLValidatingReader to do the validation, without modifying either the schema or the instance? This is .NET framework 1.1.
0
9566
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10003
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9943
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9828
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.