473,657 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

In all my Googling about this I only find examples of how to validate
an XMLReader or XmlValidatingRe ader 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"><bo ok><title>I Robot</title><author>A simov</
author></book>";
XmlDocument r = new XmlDocument();
r.LoadXml(myXml );
ValidationEvent Handler eventHandler = new
ValidationEvent Handler(Validat ionEventHandler );
r.Validate(even tHandler);

May 10 '07 #1
1 7776
engwar,

The XmlValidatingRe ader (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.co m

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

In all my Googling about this I only find examples of how to validate
an XMLReader or XmlValidatingRe ader 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"><bo ok><title>I Robot</title><author>A simov</
author></book>";
XmlDocument r = new XmlDocument();
r.LoadXml(myXml );
ValidationEvent Handler eventHandler = new
ValidationEvent Handler(Validat ionEventHandler );
r.Validate(even tHandler);

May 10 '07 #2

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

Similar topics

1
1749
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
9104
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 way as I could come up with. Yes, it's obvious I don't know what I'm doing. I'm creating an XSD string and an XML string at runtime (ie: NOT read
1
4292
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 become unacceptable. Performance improvment strategies I know of: 1. Switching to XMLReader
2
1774
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. Thanks John
0
2614
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 being entered i get plenty of warnings saying it cant find the schema details for element x). So i've come up with the following code, it works but is a pain because the document is loaded twice: XmlDocument xmlDoc = new XmlDocument();...
6
2749
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 instead. Didn't work either. Actuallty, the xml gets loaded but there are no events raised that says my xml is incorrect! I'm using .NET 2.0 and my code is listed below. ALL help is appreciated! Thanks!
10
2460
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 suggestions?
0
958
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 name="Sample"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Double" /> <xsd:enumeration value="Integer" /> </xsd:restriction>
0
1035
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 validate each lines in the Text box against the standard xsd file for suppose it should not save any invalid entries in the xml file. Is there any way to validate the XML texts directly from the RichTextBox? If this is not possile atleast i want...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.