Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top