Catch only character encoding issues in XmlException

186 Views Asked by At

I'm using System.Xml to parse xml documents. Sometimes the xml documents contain unencodable characters and then an XmlException gets thrown. In those cases, I want to retry parsing the document with a forced encoding, like this:

try {
    var doc = new XmlDocument();
    doc.Load()
} catch (XmlException xe) {
    // Retry here with another encoding..
}

This works fairly well except that XmlException gets thrown for all types of xml problems even those not caused by character encoding issues. In those cases I do not want to retry parsing. So is there a way to figure out whether the XmlException was caused by character encoding problems or something else?

2

There are 2 best solutions below

0
On BEST ANSWER

I guess the answer is no, there is no way to robustly find out what caused the XmlException.

1
On

I don't know exactly what the exception looks like, but surely by checking the contents of xe.Message or xe.InnerException you would be able to determine the type of exception?