0

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?

Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122

2 Answers2

0

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?

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
0

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

Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122