[quoted text, click to view] "Andy" <andyrich_1@hotmail.com> wrote in message news:6a33f967.0408050602.12b36ff@posting.google.com...
> I am having some trouble validating XML using the XmlValidatingReader.
: :
> The problem I am having is that the validation event fires for each
> node in the xml. It seems to be completely ignoring the schema that I
> have used.
Is what you're seeing a number of "element is not declared" errors?
[quoted text, click to view] > I'm wondering if I need to do something extra to tell the xml which
> schema to use.
Well, let's look carefully at how ValidationSchema is being created.
: :
[quoted text, click to view] > Dim ValidationSchema As New Xml.Schema.XmlSchema
> ValidationSchema.Read(ValidatorReader, AddressOf
> ValidationEventHandler)
> ValidationSchema.Compile(AddressOf ValidationEventHandler)
> colSchemas.Add(ValidationSchema)
On the first line, ValidationSchema is created as an empty schema.
On the second line, XmlSchema::Read( ) is being called to load in the
XML Schema Document contained in sSchema and _then throwing
it all away_.
On the third line, Compile( ) is happily compiling the empty schema
you created in the first line.
On the fourth line, Add( ) adds the empty schema to the collection of
schemas you later use to validate the the instance document. Of
course, since it is empty, every element encountered in the instance
document receives an "element is not declared" error.
Being an observant programmer, I'm sure your eyes bugged out when
I described what was happening on the second line, so let's go back
to that. :-)
I say "throwing it all away" because Read is a Shared Sub. Now, in
some programming languages (like C#) this is a very difficult typo to
make, because the compiler will emit an error should you call such a
static method using an instance object. However, Visual Basic is
much more forgiving, and will just assume, since ValidationSchema
IS an XmlSchema anyway, that you really wanted to call XmlSchema::
Read( ) and didn't want a diagnostic.
So, whereas from looking at the line,
ValidationSchema.Read( ValidatorReader, AddressOf veh)
you might think [where are the Cars?] the Read operation is feeding
its resulting SOM into the ValidationSchema object that you're going
to compile and add into a collection later, what is actually happening
is,
XmlSchema.Read( ValidatorReader, AddressOf veh)
XmlSchema::Read( ) returns an XmlSchema. There is no variable on
the left-hand side to receive that XmlSchema that it's read, so it just
gets thrown away.
The answer then, is to capture the XmlSchema returned by Read( ):
Dim ValidationSchema As Xml.Schema.XmlSchema ' New isn't required.
ValidationSchema = XmlSchema.Read( _
ValidatorReader, _
AddressOf ValidationEventHandler _
)
' ValidationSchema is no longer empty.
Derek Harmon