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? 3 2075
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>";
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.
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: The Plankmeister |
last post by:
Hi...
What's the best method of validating input characters? I would like to
prevent users submitting exotic characters (such as those acquired on
Windows Systems by pressing ALT+) and thought...
|
by: Daniel Ng |
last post by:
hello,
According to the Xerces-C++ FAQ, validating an DOM (not xml file) is
not
supported. One way to do it is to write the DOM to a file and
re-parse it for validation.
Is there any way to...
|
by: scorpion |
last post by:
I have this problem that an xml instance is validated correctly
by xml tools, but not with my simple code, by setting the
validating flag to true.
--------------- Schema...
|
by: Joel |
last post by:
Hi! I have a textbox and there is a validating. My question is when I quit
(Push Button) I don't want the validating proc to fire, is it possible?
|
by: SMai24 |
last post by:
Anyone has the same experience? I am trying to validate one of the
textboxes inside a datagrid, but everytime when i click my mouse on
another cell, the validating fires twice. Help plz....
|
by: Visual Systems AB \(Martin Arvidsson\) |
last post by:
Hi!
I have written a simple validating event for a couple of textbox's on a
form.
They validate just fine when leaving the form, now for the problem. If i
click a button to begin execution, i...
|
by: Chris Dunaway |
last post by:
I have a form with a textbox and numerous panels, buttons and other
controls. I have handled the textbox Validating and Validated events. The
textbox will hold a filename. In the validating...
|
by: JJ |
last post by:
Can you force a control to carrying out its validating code without having
to leave/change focus - i.e. raise its validating event ?
|
by: Darin |
last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to
handle textbox.validating, and I have the form setup so the Cancel
button is the Cancel button.
WHen the user clicks on...
|
by: Peted |
last post by:
Hi
if i derive a reference to a control on a winform
(ie Control activeControl = somecontrol on the form) how can i test
if that control has a validating or validated event and more
importantly...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |