XmlReaderSettings CheckCharacters=false 似乎不起作用 [英] XmlReaderSettings CheckCharacters=false doesn't seem to work

查看:25
本文介绍了XmlReaderSettings CheckCharacters=false 似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化来自 Rest 服务的 xml 响应.我正在实施 IXmlSerializable,因为 xml 相当具体,而且我进行了自定义序列化.响应包含非法的 xml 字符,但由于我无法修改 xml,因此我必须处理它们.

I'm trying to deserialize a xml response from a Rest service. I'm implementing IXmlSerializable because the xml is rather specific and I do custom serializing. The response contains illegal xml characters but as I have no way to modify the xml I'll have to deal with them.

解决方案似乎很简单:创建我的 XmlReader 时,我将它提供给 XmlSetting 并将 ChecCharacters 设置为 false :

The solution seems simple : when creating my XmlReader I feed it XmlSetting with ChecCharacters set to false :

XmlReaderSettings settings = new XmlReaderSettings();
settings.CheckCharacters = false;

using (var reader = XmlReader.Create(filename, settings))
{
    var xRoot = new XmlRootAttribute(RootElement);
    var serializer = new XmlSerializer(typeof(T), xRoot);
    return (T)serializer.Deserialize(reader);
}

当检查 CheckCharacters 时有效地设置为 false.

When checking the CheckCharacters is effectively set to false.

但我仍然不断收到类似的错误:

But I still keep getting errors like :

{"'', hexadecimal value 0x01, is an invalid character. Line 9, position 55."}

我认为 CheckCharacters=false 设置是为了避免由于非法 Xml 字符而引发错误?

I thought the CheckCharacters=false setting was intended to avoid throwing errors because of illegal Xml characters ?

知道我在哪里犯了错误,为什么错误不断被抛出?

Any idea where I make a mistake, why the errors keep on being thrown ?

提前谢谢..拉夫

推荐答案

来自 MSDN:

如果 XmlReader 正在处理文本数据,它总是检查 XML 名称和文本内容是否有效,而不管属性设置如何.将 CheckCharacters 设置为 false 将关闭字符实体引用的字符检查.

If the XmlReader is processing text data, it always checks that the XML names and text content are valid, regardless of the property setting. Setting CheckCharacters to false turns off character checking for character entity references.

因此将 CheckCharacters 设置为 false 将不允许您解析无效的 XML.

So setting CheckCharacters to false won't allow to you parse invalid XML.

您可以尝试用转义符替换二进制字符:'\x01'""XmlReader 与disabled CheckCharacters 似乎接受这些.

You can try to replace binary characters with escapes: '\x01' with "" etc. XmlReader with disabled CheckCharacters seems to accept those.

这篇关于XmlReaderSettings CheckCharacters=false 似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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