XmlReaderSettings CheckCharacters=false doesn't seem to work

谁说我不能喝 提交于 2019-12-10 22:14:29

问题


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.

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);
}

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."}

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 ?

thnx in advance.. Raf


回答1:


From MSDN:

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.

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

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



来源:https://stackoverflow.com/questions/21509569/xmlreadersettings-checkcharacters-false-doesnt-seem-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!