XML异常:无效字符(S) [英] XML Exception: Invalid Character(s)

查看:254
本文介绍了XML异常:无效字符(S)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作是从一个长期运行的应用程序接收字符串形式的XML数据一个小项目。我想这串数据加载到一个的XDocument System.Xml.Linq.XDocument ),然后从还有做一些XML魔术和创造的数据报告XLSX文件。



在的场合,我收到了无效的XML字符数据,试图解析时串入一个的XDocument ,我得到这个错误。




[的System.Xml .XmlException]
消息:'?',十六进制值0x1C处,是无效字符




由于我没有控制在远程应用程序,你可以指望什么样的性格。



我很清楚地知道,XML提供了一个方法,你可以把字符就如&放大器;#X1C 或类似的东西。



如果可能的话我会认真想保持所有数据。如果不是这样,不是顺其自然。






我曾经想过编程编辑响应字符串,然后回去和努力重新解析应该抛出异常,但我已经尝试了一些方法和他们都不成功。



感谢您的想法。



代码是沿着这一行的内容:

  TR TextReader的; 
的XDocument文档;

串响应;从服务器接收到// XML字符串。
...
TR =新StringReader(响应);


{$ B $(DOC)B = XDocument.Load(TR);
}
赶上(XmlException E)
{
//处理吗?
}


解决方案

XML可以几乎任何处理性格,但也有范围,控制代码和这样,它不会。



您最好的选择,如果你不能让他们解决他们的输出,是净化您收到的原始数据。你需要用您注意字符参照形式非法字符。



(你甚至不能诉诸CDATA,因为没有办法逃脱存在这些字符)。


I am working on a small project that is receiving XML data in string form from a long running application. I am trying to load this string data into an XDocument (System.Xml.Linq.XDocument), and then from there do some XML Magic and create an xlsx file for a report on the data.

On occasion, I receive the data that has invalid XML characters, and when trying to parse the string into an XDocument, I get this error.

[System.Xml.XmlException] Message: '?', hexadecimal value 0x1C, is an invalid character.

Since I have no control over the remote application, you could expect ANY kind of character.

I am well aware that XML has a way where you can put characters in it such as &#x1C or something like that.

If at all possible I would SERIOUSLY like to keep ALL the data. If not, than let it be.


I have thought about editing the response string programatically, then going back and trying to re-parse should an exception be thrown, but I have tried a few methods and none of them seem successful.

Thank you for your thought.

Code is something along the line of this:

TextReader  tr;
XDocument  doc;

string           response; //XML string received from server. 
... 
tr = new StringReader (response);   

try
{
    doc = XDocument.Load(tr);
}
catch (XmlException e)
{
    //handle here?
}

解决方案

XML can handle just about any character, but there are ranges, control codes and such, that it won't.

Your best bet, if you can't get them to fix their output, is to sanitize the raw data you're receiving. You need replace illegal characters with the character reference format you noted.

(You can't even resort to CDATA, as there is no way to escape these characters there.)

这篇关于XML异常:无效字符(S)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆