0
votes

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

3

3 Answers

1
votes

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.

1
votes

you can use an Xsl to reformat your xml before the deserialization

EDIT

for xsl transform with c#: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

your xsl should contains something like that

<xsl:template match="autorizacion">
  <autorizacion><xsl:value-of select="concat(upper-case(substring(current(),1,1)),           substring(current(),2))" />
</autorizacion>
</xsl:template>
0
votes

you'll have to format it without validating it to your schema. format the values that are wrong and revalidate it with schema.