Groups | Blog | Home
all groups > dotnet xml > december 2003 >

dotnet xml : Validation problem: is it a bug?


Fede
12/4/2003 11:58:01 AM
Hi to all,
I've written a simple XML file and I'm trying to validate it using
XMLValidatingReader and ValidationEventHandler event.

This is my code:

Imports System.Xml
Imports System.Xml.Schema

Module Module1
Dim IsValid As Boolean = True
Sub main()
Dim filename As String = "Example.xml"
Dim TReader As New XmlTextReader(filename)
Dim VReader As XmlValidatingReader = New
XmlValidatingReader(TReader)
VReader.ValidationType = ValidationType.DTD
AddHandler VReader.ValidationEventHandler, AddressOf
myValidationEventHandler

While VReader.Read()
End While

TReader.Close()
VReader.Close()
MsgBox(IsValid.ToString)
End Sub
Private Sub myValidationEventHandler(ByVal Sender As Object, ByVal args
As ValidationEventArgs)
IsValid= False
End Sub
End Module


This is the file Example.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--DTD-->
<!DOCTYPE Books [
<!ELEMENT Books (Book)+>
<!ELEMENT Book (author, summary)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT summary (#PCDATA | %reference;)*>
<!ELEMENT ref (#PCDATA)>
<!ATTLIST Book
title CDATA #REQUIRED
[quoted text, click to view]
<!--entities-->
<!ENTITY % reference "ref">
]>
<!--End Of DTD-->
<Books>
<Book title="The Hound of the Baskerville">
<author>Arthur Conan Doyle</author>
<summary>It's a tale of Sherlock Holmes by Sir <ref>Arthur Conan
Doyle</ref>, was first published in serial form in 1901, then in book form
in 1902. It's the story of an age-old curse and it's ramifications to the
Bakerville family. <ref>Sherlock Holmes</ref> and <ref>Dr. Watson</ref> are
on the case!</summary>
</Book>
<Book title="Murders on the Rue Morgue">
<author>Edgar Allan Poe</author>
<summary>In <ref>Poe</ref>'s "Murders on the Rue Morgue," his
fictional detective <ref>C. Auguste Dupin</ref>, together with Dupin's
friend and narrator, read in the newspaper of two particularly gruesome
murders.</summary>
</Book>
</Books>

This file is valid and my procedure's result is "True". But if you remove
the end tag </ref>, for example, the file will not be valid but my
procedure's result will be "True"!!!!
Why? Is it perhaps a bug? Has someone any ideas?
Thanks in advance,
Fede.

Oleg Tkachenko
12/4/2003 1:22:11 PM
[quoted text, click to view]

Removing end tag makes XML non-wellformed. Nothing can be done with
non-wellformed XML, XmlReader just throws an exception prior to
XmlValidatingReader can validate.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Fede
12/4/2003 3:43:44 PM
[quoted text, click to view]

So I have to read my file twice: one with a XMLTextReader (to verify if my
file is well-formed) and one with a XmlValidatingReader.
Is it possible to read the file only once?
If it is, can you give me an example?

Thanks in advance.
Fede

Oleg Tkachenko
12/4/2003 6:51:50 PM
[quoted text, click to view]

Well, not really. XmlValidatingReader works on top of XmlTextReader.
XmlTextReader parses XML syntax, verifies well-formdness and
XmlValidatingReader validates information reported by XmlTextReader.
Probably you probllem is that you don't catch the exception
XmlTextReader throws in the case of ill-formed XML.

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
AddThis Social Bookmark Button