views:

947

answers:

1

I'm going to ask and answer my own question, I hope nobody minds but I thought this might be useful to other people.

If you setup a ASP.NET Web Service that returns objects that contain characters that are invalid for XML an exception will be thrown after the object is serialized in to SOAP xml and the client attempts to deserialize that xml.

How do you fix this?

+5  A: 

To fix this I generated the class file for my webservice with the wsdl.exe application that is part of .NET. This is straight forward to do, at a command prompt just type wsdl.exe <path to webservice>

After that is generated I overloaded the method

protected XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)

like this

protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)
{
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.CheckCharacters = false;
    return XmlTextReader.Create(message.Stream, settings);
}

This tells the XmlTextReader to ignore the validity of the XML file it is reading. There's no reason that I care if the xml is valid or not when I'm just going to immediately deserialize it.

Hope this helps someone with the same problem out there!

Boog