When attempting to validate an XML file, If the file is valid, it
validates correctly, and it will catch most ValidationEvents without
problem. However, I keep getting the following Exception:
System.Xml.XmlException: 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.XmlTextReader.ParseTag()
at System.Xml.XmlTextReader.ParseBeginTagExpandCharEn tities()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlValidatingReader.ReadWithCollectText Token()
at System.Xml.XmlValidatingReader.Read()
at XMLValidator.XMLEvaluator.Evaluate(String configPath) in
c:\documents and settings\gwalker\my documents\visual studio
projects\xmlvalidator\xmlevaluator.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(configPath);
XmlValidatingReader val = new XmlValidatingReader(evaluated);
try
{
Console.WriteLine("Validating XML file ");
// Set the validation event handler
val.ValidationEventHandler += new
ValidationEventHandler(this.ValidationEventHandle) ;
// Read XML data
while (val.Read()){}
Console.WriteLine ("Validation finished. Validation {0}",
(m_success==true ? "successful" : "failed"));
}
finally
{
//Close the reader.
if (val != null)
val.Close();
}
}// end of Evaluate method
private void ValidationEventHandle (object sender, ValidationEventArgs
args)
{
m_success = false;
Console.WriteLine("Validation error: " + args.Message );
}