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?
The only illegal characters are & , < and > (as well as " or ' in attributes, depending on which character is used to delimit the attribute value: attr="must use " here, ' is allowed" and attr='must use ' here, " is allowed' ). They're escaped using XML entities, in this case you want & for & .
Some special characters are not permitted in XML attribute values. Note that the ampersand (&) and less-than (<) characters are not permitted in XML attribute values.
The XML functions of the database server automatically handle special characters. When a SQL result set contains special characters, the XML function will automatically handle it. These special characters are listed in the following table.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With