Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I set up my Xml validation like this...

C#
XmlTextReader xrdr = new XmlTextReader(tempFile);
XmlValidatingReader xvr = new XmlValidatingReader(xrdr);
xvr.ValidationType = ValidationType.Schema;
xvr.ValidationEventHandler += new ValidationEventHandler(FormValidationEventHandler);
while (xvr.Read())
{
}


And my FormValidationEventHandler(object sender, ValidationEventArgs args) event handler is simply trying to capture the most meaningful message for the user, which should include the failing Xml node's name and value, along with the exception message. However, when pausing the debugger in the event handler, only the internal variable $exception contains the specific node value in the message. The args.Message and args.Exception.Message both contain only the node name, which is not helpful enough. I have explored all the properties and methods of the sender and args, but have not found it.

Here is an example of the message currently available...

"The 'http://www.sec.gov/edgar/nmfpsecurities:InvestmentIssuer' element has an invalid value according to its data type."

Here is an example of the message in $exception...

"The value 'E.I. DU PONT DE NEMOURS & CO. ' is invalid according to its schema type 'http://www.sec.gov/edgar/invest:InvestmentIssuer' - line-feed (#xA) or tab (#x9) characters, leading or trailing spaces and sequences of one or more spaces (#x20) are not allowed in 'xs:token'."

I need to know how to either 1) access the failing node value given the sender and args, or 2) magically access the internal variable $exception inside my event handler. Thanks!
Posted
Updated 8-Nov-10 7:55am
v4

1 solution

Found the answer myself... I noticed that XmlValidatingReader was deprecated, so when I upgraded the code, I got the result that I wanted...

System.Xml.Schema.XmlSchema schema = System.Xml.Schema.XmlSchema.Read(new XmlTextReader(schemaPath), null);
XmlDocument doc = new XmlDocument();
doc.Schemas.Add(schema);
doc.Load(tempFile);
ValidationEventHandler veh = new ValidationEventHandler(FormValidationEventHandler);
doc.Validate(veh);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900