468,268 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

Validating in-memory XML against in-memory DTD?

Hi

I need to validate an in-memory XML document against an in-memory DTD.
I've tried using XmlValidatingReader and it works but the compiler
complains that the class is obsolete. Here's the code:

string dtd = "<!ELEMENT bookstore (book)*> " +
"<!ELEMENT book (title,author*,price)>" +
"<!ATTLIST book genre CDATA #REQUIRED>" +
"<!ELEMENT title (#PCDATA)>" +
"<!ELEMENT author (name |
(first-name,last-name))>" +
"<!ELEMENT price (#PCDATA)>" +
"<!ELEMENT name (#PCDATA)>" +
"<!ELEMENT first-name (#PCDATA)>" +
"<!ELEMENT last-name (#PCDATA)>";

// Should fail validation - missing genre and title
string xmlDef = "<?xml version='1.0'?>" +
"<bookstore>" +
" <book>" +
" <author>" +
" <first-name>Benjamin</first-name>" +
" <last-name>Franklin</last-name>" +
" </author>" +
" <price>8.99</price>" +
" </book>";

bool validXml = true;

XmlParserContext parserContext = new XmlParserContext(null, null,
"bookstore", "", "", dtd, "", null, XmlSpace.Default);
XmlValidatingReader reader = new XmlValidatingReader(xmlDef,
XmlNodeType.Document, parserContext);
reader.ValidationEventHandler += delegate(object o, ValidationEventArgs
args)
{
validXml = false;
};

while (validXml && reader.Read()){}

return validXml;

This version works fine. However, as soon as I change the code to
conform to .NET 2.0, the validation stops working.

bool validXml = true;

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;

XmlParserContext parserContext = new XmlParserContext(null, null,
"bookstore", "", "", dtd, "", null, XmlSpace.Default);
XmlReader reader = XmlReader.Create(new StringReader(xmlDef), settings,
parserContext);

settings.ValidationEventHandler += delegate(object o,
ValidationEventArgs args)
{
validXml = false;
};

while (validXml && reader.Read()){}

Is this a bug or am I not doing something correctly?

Jun 2 '06 #1
3 1932
I just noticed the code snippet is missing the closing <bookstore> tag.
This:
string xmlDef = "<?xml version='1.0'?>" +
"<bookstore>" +
" <book>" +
" <author>" +
" <first-name>Benjamin</first-name>" +
" <last-name>Franklin</last-name>" +
" </author>" +
" <price>8.99</price>" +
" </book>";


should be:
string xmlDef = "<?xml version='1.0'?>" +
"<bookstore>" +
" <book>" +
" <author>" +
" <first-name>Benjamin</first-name>" +
" <last-name>Franklin</last-name>" +
" </author>" +
" <price>8.99</price>" +
" </book>" +
"</bookstore>";

Jun 2 '06 #2
OK, I seemed to have found one problem. The ValidationEventHandler
needs to be set before the XmlReader is created.

It still doesn't work though. It now complains that the DTD is not
found.

Jun 2 '06 #3
Not sure why it doesn't work in .Net 2.0. I will forward your question to
people who can answer your question.

Thanks,
Junfeng

<sa****@gmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
Hi

I need to validate an in-memory XML document against an in-memory DTD.
I've tried using XmlValidatingReader and it works but the compiler
complains that the class is obsolete. Here's the code:

string dtd = "<!ELEMENT bookstore (book)*> " +
"<!ELEMENT book (title,author*,price)>" +
"<!ATTLIST book genre CDATA #REQUIRED>" +
"<!ELEMENT title (#PCDATA)>" +
"<!ELEMENT author (name |
(first-name,last-name))>" +
"<!ELEMENT price (#PCDATA)>" +
"<!ELEMENT name (#PCDATA)>" +
"<!ELEMENT first-name (#PCDATA)>" +
"<!ELEMENT last-name (#PCDATA)>";

// Should fail validation - missing genre and title
string xmlDef = "<?xml version='1.0'?>" +
"<bookstore>" +
" <book>" +
" <author>" +
" <first-name>Benjamin</first-name>" +
" <last-name>Franklin</last-name>" +
" </author>" +
" <price>8.99</price>" +
" </book>";

bool validXml = true;

XmlParserContext parserContext = new XmlParserContext(null, null,
"bookstore", "", "", dtd, "", null, XmlSpace.Default);
XmlValidatingReader reader = new XmlValidatingReader(xmlDef,
XmlNodeType.Document, parserContext);
reader.ValidationEventHandler += delegate(object o, ValidationEventArgs
args)
{
validXml = false;
};

while (validXml && reader.Read()){}

return validXml;

This version works fine. However, as soon as I change the code to
conform to .NET 2.0, the validation stops working.

bool validXml = true;

XmlReaderSettings settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
settings.ValidationType = ValidationType.DTD;

XmlParserContext parserContext = new XmlParserContext(null, null,
"bookstore", "", "", dtd, "", null, XmlSpace.Default);
XmlReader reader = XmlReader.Create(new StringReader(xmlDef), settings,
parserContext);

settings.ValidationEventHandler += delegate(object o,
ValidationEventArgs args)
{
validXml = false;
};

while (validXml && reader.Read()){}

Is this a bug or am I not doing something correctly?

Jun 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by The Plankmeister | last post: by
reply views Thread by Daniel Ng | last post: by
1 post views Thread by scorpion | last post: by
2 posts views Thread by Joel | last post: by
1 post views Thread by Visual Systems AB \(Martin Arvidsson\) | last post: by
2 posts views Thread by Chris Dunaway | last post: by
4 posts views Thread by JJ | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.