martedì 28 febbraio 2012 18:43
What would be the best way to validate an XML file against an XSD and be able to provide localized error messages to the user?
The examples I saw online point to XmlReader and XmlReaderSettings but this approach yields only an English message. It doesn't provide the necessary information for me to construct a message in another language, like ErrorCode, Element Name, Element Value, or something like that.
Any thoughts would be appreciated,
Tutte le risposte
mercoledì 29 febbraio 2012 11:49
First I would check if XmlReader does not output error message in any of the .NET framework's language packs that you can install: http://www.microsoft.com/download/en/details.aspx?id=21891.
As far as I am aware XmlReader implementation do not provide any error code (and I think other APIs in the .NET framework don't do that either). As for getting the element name, in your validation event handler you can cast the sender argument to an XmlReader with e.g.
XmlReader xr = sender As XmlReader;
if (xr != null)
// now access xr.Name, LocalName etc here
// note that the XmlReader is usually positioned on an end element when it reports an invalid element value
Note that you can also validate in-memory object models like System.Xml.XmlDocument or like System.Xml.Linq.XDocument, that way accessing and reporting invalid nodes is easier compared to the forwards only XmlReader validation (which however consumes much less memory).
MVP Data Platform Development My blog
giovedì 1 marzo 2012 16:56
Thank you Martin.
Casting the XMLReader in the Validation Error Event was useful.
I noticed that the parameter Arg in the event has an Exception object and it carries an hResult (same for the Inner Exception). Maybe that could be interpreted as an Error Code?