473,509 Members | 11,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

validating XML with XmlValidatingReader

I have a doc that that I am entering an incorrect value in a node that
expects an restricted enumeration yet I can't get the ValidationEventHandler
to fire. Shouldn't it fire if it finds an enumeration that's not listed in
the schema? I've also misspelled node tags and it still won't fire. Am I
missing something? Thanks.

Public Shared Function IsValid(ByVal cXML As String) As Boolean

_valid = True

Dim schemaColl As XmlSchemaCollection

schemaColl = New XmlSchemaCollection

schemaColl.Add(Nothing, New XmlTextReader("..\myXSD.xsd"))

Dim ioStream As Stream

ioStream = New MemoryStream(ASCIIEncoding.Default.GetBytes(cXML))

Dim xmlReader As XmlTextReader

xmlReader = New XmlTextReader(ioStream)

Dim xmlValidator As XmlValidatingReader

xmlValidator = New XmlValidatingReader(xmlReader)

xmlValidator.ValidationType = ValidationType.Schema

xmlValidator.Schemas.Add(schemaColl)

AddHandler xmlValidator.ValidationEventHandler, AddressOf ValidationError

While xmlValidator.Read()

End While

Return _valid

xmlReader.Close()

xmlValidator.Close()

End Function

Private Shared Sub ValidationError(ByVal sender As Object, ByVal args As
ValidationEventArgs)

_valid = False

End Sub
Feb 13 '06 #1
1 1236
prefersgolfing wrote:
I have a doc that that I am entering an incorrect value in a node that
expects an restricted enumeration yet I can't get the ValidationEventHandler
to fire. Shouldn't it fire if it finds an enumeration that's not listed in
the schema? I've also misspelled node tags and it still won't fire. Am I
missing something? Thanks.


I've not used the validating reader. Perhaps this sample method (that
does fire events) will help:

private void checkSchema( string document )
{
XmlReader schemaReader = null;
XmlReader docReader = null;
MemoryStream ms = null;

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += new
System.Xml.Schema.ValidationEventHandler( schemaValidationEventHandler );
settings.ValidationType = ValidationType.Schema;

try
{
ms = /* my method to convert string to a mem stream */;
string schemaPath = /* my method to get path */;
schemaReader = XmlReader.Create( schemaPath );
settings.Schemas.Add( null, schemaReader );
docReader = XmlReader.Create( ms, settings );
while ( docReader.Read() ) { };
}
finally
{
schemaReader.Close();
docReader.Close();
if ( ms != null ) { ms.Close(); }
}
}
Feb 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4039
by: Adam Child | last post by:
Hi All, I'm trying to validate an xml document. I'm having trouble setting the default namespace of the xml document. If I hard encode the namespace in the xml file then everything works fine....
1
1818
by: Ryan | last post by:
I have a very complex XDR schema that uses namespaces: xmlns="urn:schemas-microsoft-com:xml-data" xmlns:b="urn:schemas-microsoft-com:BizTalkServer" xmlns:d="urn:schemas-microsoft-com:datatypes"...
2
2614
by: Raghu R | last post by:
From: "R. Raghuraman" <raghuraman@infosys.com> Subject: Problem with XMLValidatingReader? Date: 01 April 2004 11:46 Hi, I have a large xml file to process (~ 70 mb). I am trying to use...
6
3379
by: Robert Reineri | last post by:
Hello, New to the XML world and .NET. I have what I believe to be a simple problem, but I have read the .NET docs till I'm blue in the face and still can't locate a simple example of how to...
2
4136
by: dotNetty | last post by:
Hi I have a problem with validating WSDL-Files using XMLValidatingReader. I'm adding all schemas in the <definitions>-Tag to the XmlValidatingReader.Schemas Property. and here is the Problem:...
4
1879
by: Tomas Rivas | last post by:
I am trying to validate an xml file and schema and when I am trying to validate I am getting the following error. I have been trying to come out with a solution but I have failed so far. The...
1
2945
by: Andy | last post by:
I am having some trouble validating XML using the XmlValidatingReader. I have created some xml and used the visual studio to generate the schema. So I am confident that the xml and schema match. ...
2
2613
by: Joris Janssens | last post by:
I'm trying to write a program for validating XHTML 1.1-documents against the XHTML 1.1 DTD (which is actually the same as validating an XML-file) but I always get a "(404) Not found" error. This...
1
4207
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
2
7138
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
1
7074
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
7506
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
5659
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,...
1
5063
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...
0
3219
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
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
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...

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.