I’m calling a “web service” that gives me as an xml response an invalid node, so when I try to deserialize it, it throws an exception.
I’m using the XmlSerializer class, like so:
internal class Response<T>
{
public Response(byte[] xml)
{
XmlSerializer s = new XmlSerializer(typeof(T));
XmlReader reader = XmlReader.Create(new MemoryStream(xml));
if (s.CanDeserialize(reader))
this.ActualResponse = (T)s.Deserialize(reader);
}
public T ActualResponse { get; private set; }
}
and the node I’m having trouble with looks something like this:
<autorizacion>FALSE</autorizacion>
The exception I get is
System.InvalidOperationException:
There is an error in XML document (7,
35). —> System.FormatException: The
string ‘FALSE’ is not a valid Boolean
value..
Which is obvious.
The question is, how can I deserialize it without having to iterate through all nodes, building my response entity by hand? Is there a way?
I don’t have control over the server
Quickest way seems to be to change the parameter of setAutorizacion(boolean) to setAutorizacion(String), then convert to a boolean in the setter. Also, document what you did and why you did it both in that setter and in more high-level documentation.