如何捕获从第三方库抛出的XmlException?

时间:2012-07-13 13:34:11

标签: c# exception exchangewebservices

我正在使用Exchange Web服务托管API,大约每天抛出XmlException我不知道如何捕获。以下是例外情况。

System.Xml.XmlException was unhandled
  Message="'', hexadecimal value 0x1F, is an invalid character. Line 1, position 1."
  Source="System.Xml"
  LineNumber=1
  LinePosition=1
  SourceUri=""
  StackTrace:
       at System.Xml.XmlTextReaderImpl.Throw(Exception e)
       at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
       at System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
       at System.Xml.XmlTextReaderImpl.ThrowInvalidChar(Int32 pos, Char invChar)
       at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
       at System.Xml.XmlTextReaderImpl.ParseText()
       at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
       at System.Xml.XmlTextReaderImpl.Read()
       at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read()
       at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read(XmlNodeType nodeType)
       at Microsoft.Exchange.WebServices.Data.EwsXmlReader.InternalReadElement(XmlNamespace xmlNamespace, String localName, XmlNodeType nodeType)
       at Microsoft.Exchange.WebServices.Data.EwsXmlReader.ReadStartElement(XmlNamespace xmlNamespace, String localName)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadResponse(EwsServiceXmlReader ewsXmlReader)
       at Microsoft.Exchange.WebServices.Data.HangingServiceRequestBase.ParseResponses(Object state)
       at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)
  InnerException: 

从堆栈跟踪中我看不到任何可以让我捕获并修复问题的内容。

我怎么能抓住这个?

1 个答案:

答案 0 :(得分:2)

您的XML库不支持0x1F char,它是单位分隔符 char。

您可以做的是在您的流/ XML文件/ XML库处理的任何/处理之前,将所有出现的特定字符转义为。 见How do I escape unicode character 0x1F in xml?

请参阅allowed character ranges for XML 1.1