473,748 Members | 3,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlValidatingRe ader: continue parsing after XmlException

When attempting to validate an XML file, If the file is valid, it
validates correctly, and it will catch most ValidationEvent s without
problem. However, I keep getting the following Exception:
System.Xml.XmlE xception: The 'RDaaaa' start tag on line '4' doesn't
match the end tag of 'RD' in file 'file:///C:/Work/WIMAR
XML/WI_M_1.xml'. Line 4, position 475.
at System.Xml.XmlT extReader.Parse Tag()
at System.Xml.XmlT extReader.Parse BeginTagExpandC harEntities()
at System.Xml.XmlT extReader.Read( )
at System.Xml.XmlV alidatingReader .ReadWithCollec tTextToken()
at System.Xml.XmlV alidatingReader .Read()
at XMLValidator.XM LEvaluator.Eval uate(String configPath) in
c:\documents and settings\gwalke r\my documents\visua l studio
projects\xmlval idator\xmlevalu ator.cs:line 43

Well, this technically is doing what it's supposed to be doing, but I'd
rather have XmlExceptions fired as Events rather than throwing
exceptions. With a try-finally block, I want to easily be able to get
back into reading the XML file after an exception like this. (I am sort
of a newbie in all this, so apologies if this is a really really simple
question.)

I know that my XML and schema are valid. I introduced the error for
testing purposes.
I'm using the following example code from MSDN:
public void Evaluate(string configPath)
{
/**
* if file is okay, return 1
* if file is not okay, return error string
*/
XmlTextReader evaluated = new XmlTextReader(c onfigPath);
XmlValidatingRe ader val = new XmlValidatingRe ader(evaluated) ;
try
{
Console.WriteLi ne("Validating XML file ");

// Set the validation event handler
val.ValidationE ventHandler += new
ValidationEvent Handler(this.Va lidationEventHa ndle);

// Read XML data
while (val.Read()){}

Console.WriteLi ne ("Validation finished. Validation {0}",
(m_success==tru e ? "successful " : "failed"));
}
finally
{
//Close the reader.
if (val != null)
val.Close();
}
}// end of Evaluate method

private void ValidationEvent Handle (object sender, ValidationEvent Args
args)
{
m_success = false;
Console.WriteLi ne("Validation error: " + args.Message );
}
Nov 12 '05 #1
1 2640
>Well, this technically is doing what it's supposed to be doing, but I'd
rather have XmlExceptions fired as Events rather than throwing
exceptions. With a try-finally block, I want to easily be able to get
back into reading the XML file after an exception like this. (I am sort
of a newbie in all this, so apologies if this is a really really simple
question.)


The Validation event handler only catches violations of the schema in the
XML document. But the Xml Document need to be well formed to begin with.
Parsing can NOT continue after encountering a well formedness errors.
Nov 12 '05 #2

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

Similar topics

0
1880
by: Nick Finch | last post by:
Hi, I wonder if anyone else has come across this. The code below is very simple however I cannot get the ValidationHandler to be raised. I am testing with xml that I know will not validate against the XmlSchema that I have added to the Schemas collection of the XmlValidatingReader. I also know that the schema is sound, if I comment out the delegate assignment and wrap the code in a try/catch then I get the exception that I expect.
2
1811
by: matthew.schneider | last post by:
I have a defined XML Schema, and XML documents containing data that I validate against it. Periodically, I will have a data document that will contain extra elements that are not part of my schema. The powers that be have determined that this is acceptable. I am looking for the best way to continue to validate my documents against my schema, but ignore any errors dealing with extra elements. My first thought is to use a...
5
1412
by: Geoff | last post by:
I am using an XMLValidatingReader to validate an XML file received via a web service. I want to verify that the incoming file matches the XML schema. When testing the validation routine, the XMLValidatingReader correctly flags mis-matched tags such as <abc>some content</xyz> but does not catch other errors. For example, it doesn't catch tags that are not part of the schema, doesn't catch missing tags where the schema has minoccurs="1", and...
1
1981
by: rampm2007 | last post by:
Hi, I am using the XmlTextReader along XmlValidatingReader for parsing the XML file. Becuase of the foreign characters sometimes I get the "There is an invalid character in the given encoding" error. I know this is the issue of Encoding and when I change the Encoding, my Parser works just fine. For reading & moving to next node, I use Read method of XmlValidatingReader. What I am trying is in case if error occurs how can I ignore or handle...
0
8991
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
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
9541
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...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
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
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
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

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.