473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to validate XMLDocument (not XMLReader) against a DTD?

The title says it all. How do I use XMLDocument.Validate() against a
DTD.

In all my Googling about this I only find examples of how to validate
an XMLReader or XmlValidatingReader against a DTD.

The reason I want to avoid the readers is that I want an XmlDocument
that is not forward-only so I can loop through the doc again after
validating it.

I want to do something like the following but I keep getting the error
"the XmlSchemaSet on the document is either null or has no schemas in
it. Provide schema information before calling Validate."

What am I doing wrong?

string myXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!DOCTYPE
book SYSTEM \"book.dtd"><book><title>I Robot</title><author>Asimov</
author></book>";
XmlDocument r = new XmlDocument();
r.LoadXml(myXml);
ValidationEventHandler eventHandler = new
ValidationEventHandler(ValidationEventHandler);
r.Validate(eventHandler);

May 10 '07 #1
1 7755
engwar,

The XmlValidatingReader (which is obsolete, you should use the Create
method on the XmlReader class to create an XmlReader to do this for you)
derives from XmlReader. You can pass this reader to the Load method of the
XmlDocument class when it loads and have the reader do the validation for
you.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"engwar" <en*****@yahoo.comwrote in message
news:11**********************@e51g2000hsg.googlegr oups.com...
The title says it all. How do I use XMLDocument.Validate() against a
DTD.

In all my Googling about this I only find examples of how to validate
an XMLReader or XmlValidatingReader against a DTD.

The reason I want to avoid the readers is that I want an XmlDocument
that is not forward-only so I can loop through the doc again after
validating it.

I want to do something like the following but I keep getting the error
"the XmlSchemaSet on the document is either null or has no schemas in
it. Provide schema information before calling Validate."

What am I doing wrong?

string myXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><!DOCTYPE
book SYSTEM \"book.dtd"><book><title>I Robot</title><author>Asimov</
author></book>";
XmlDocument r = new XmlDocument();
r.LoadXml(myXml);
ValidationEventHandler eventHandler = new
ValidationEventHandler(ValidationEventHandler);
r.Validate(eventHandler);

May 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: QuantDev | last post by:
Hi NG, I would need to validate an XML fragment against a type defined within an XSD (which defines many other things). What is the correct way of achieving this? QD2004
1
by: aevans1108 | last post by:
Greetings All If this is the wrong place to post this question, please give me a push in the right direction. Thanks. I know there has to be a simpler way to do this, but this is as simple a...
1
by: John A Grandy | last post by:
I've got an app that has hundreds of medium-sized (100s of elements) XML files on disk (not in db). Right now these are loaded via XMLDocument.Load and searched with XPATH. The performance has...
2
by: John H | last post by:
Hi, How can i just use the XmlDocument object to validate an xml instanace against a schema referenced inside the xml instance? The Load method seems to not validate it against the schema. ...
0
by: Mark Ingram | last post by:
Hi, i want to ensure that an xml file has been created properly, but i dont want to rely on the creator of the xml file to correctly enter the required namespaces (without the correct namespaces...
6
by: Jonny | last post by:
Hi, I'm trying to validate my xml against a xsd but I can't get it to work. Originally, I wanted to validate an xml string but since I didn't get that to work I tried to validate an xml file...
10
by: Thirsty Traveler | last post by:
All of the XmlReader examples assume that the XmlDocument is being read from a file. However, I would like to use it with an XmlDocument passed as a method parameter, but am not sure how. Any...
0
by: hufaunder | last post by:
I want to validate an xml document with an xsd reference document. I am able to validate the tags but not the values. Here is an example: I define the following type: <xsd:simpleType...
0
by: anupam roy | last post by:
Hi, I have created my own XML text editor and using XmlReader to read the xml files I have opened one xml file for editing in the text box editor( i have . Now before saving the file, i want to...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
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...
0
marktang
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,...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.