使用EWS阅读电子邮件时处理非法XML值

gcs*_*cso 10 c# xml exchangewebservices

我们有一个应用程序,它使用a StreamingSubscriptionConnection来读取发送到特定邮箱的封电子邮件.我在开发过程中每天运行几次的问题我得到了例外{"'{square character}', hexadecimal value 0x1F, is an invalid character. Line 1, position 1."}.

这是堆栈跟踪:

   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)
Run Code Online (Sandbox Code Playgroud)

如何安全地阅读包含非法字符的EWS电子邮件?

经过一番搜索似乎它可以解决这个问题,使用旧版本的EWS API的.但是,使用最新版本的托管API似乎没有人修复.

这是来自http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/22863099-1d93-47ac-a11b-08c6bf7facea的交叉帖子.

我已经设法再次获得异常,这里是完整的堆栈跟踪以及Exchange作为通知转向的内容.

我正在使用Exchange 2010 SP1.

EWS通知异常

编辑:我正在复兴这个问题,因为它引起了我严重的问题,原始问题清楚地说明了问题.我正在寻找修改Managed EWS API行为的客户端解决方案,以便从XML中过滤无效字符并避免异常.Exchange服务器修复程序不太可能是一个选项,除非它们是简单的配置更改.我的软件将针对我无法控制的客户Exchange服务器运行.

小智 1

如果无法解决接收无效 XML 文件的问题,一般的解决方法是编写一个实用程序,将收件箱文件夹作为后台进程进行监视:

该程序将执行以下操作:

您应该能够使用此类转义序列安全地替换US-ASCII 32 以下的所有不可打印字符,但“\r”、“\n”和“\t”除外。但是,您还必须确保永远不会损坏 XML 文件,并且无论系统使用这些文件,更改后的 XML 文件仍然可以使用。

或者寻找更常见的 XML 清理程序库之一。