开发者

Catch only character encoding issues in XmlException

开发者 https://www.devze.com 2023-03-31 23:50 出处:网络
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

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 t开发者_Go百科here a way to figure out whether the XmlException was caused by character encoding problems or something else?


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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消