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 2090
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: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |