coach_rob
coach_rob

Reputation: 901

What is wrong with this C# XML Deserialization code?

I've read as many article as I can find, I've done serialization this way before but the life of me I cannot figure out why the following isn't working correctly.

Assistance appreciated.

Given this code:

POCO classes for deseralized response:

[XmlRoot]
public class MyResponseClass
{
    [XmlElement("response")]
    public Response Response { get; set; }
}

[XmlRoot]
public class Response
{
    [XmlElement("error")]
    public Error Error { get; set; }

    [XmlElement("sid")]
    public string Sid { get; set; }
}

[XmlRoot]
public class Error
{
    [XmlElement("error_msg")]
    public string ErrorMessage { get; set; }
}

Request and response handling code:

var webClient = _webClientWrapperFactory.Create();
var url = "https://serviceendpoint.com";
var responseString = webClient.DownloadString(url);

MyResponseClass result;
var serializer = new XmlSerializer(typeof(MyResponseClass));
using (var reader = new StringReader(responseString))
{
result = (MyResponseClass) serializer.Deserialize(reader);
}
return result;

Returns this response:

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><response><error><error_msg>CANNOT_LOGIN</error_msg></error></response>

Deserialization attempt throws this error:

An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code

Additional information: There is an error in XML document (1, 57).

EDIT: Inner Exception:

{"<response xmlns=''> was not expected."}

Upvotes: 0

Views: 1050

Answers (1)

I created a quick instance of MyResponseClass and serialized it:

<?xml version="1.0" encoding="utf-16"?>
<MyResponseClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <response>
    <error>
      <error_msg>error message text</error_msg>
    </error>
    <sid>(sid string)</sid>
  </response>
</MyResponseClass>

So that's what your XML should look like for MyResponseClass. Here's the serialization code I used:

MyResponseClass result1 = new MyResponseClass
{
    Response = new Response
    {
        Sid = "(sid string)",
        Error = new Error
        {
            ErrorMessage = "error message text"
        }
    }
};

var serializer = new XmlSerializer(typeof(MyResponseClass));
string serialized = "";

using (StringWriter textWriter = new StringWriter())
{
    serializer.Serialize(textWriter, result1);
    serialized = textWriter.ToString();
}

But that's probably not what you want.

If you want to stick with the XML you have in your question, you need to dispense with MyResponseClass and specify that Response, as an XML root, is all lowercase:

[XmlRoot("response")]
public class Response
{
    [XmlElement("error")]
    public Error Error { get; set; }

    [XmlElement("sid")]
    public string Sid { get; set; }
}

...

var responseString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><response><error><error_msg>CANNOT_LOGIN</error_msg></error></response>";

var serializer2 = new XmlSerializer(typeof(Response));

//using (StringWriter textWriter = new StringWriter())
//{
//    serializer2.Serialize(textWriter, result1.Response);
//    responseString = textWriter.ToString();
//}

Response result;
using (var reader = new StringReader(responseString))
{
    result = (Response)serializer2.Deserialize(reader);
}

Upvotes: 1

Related Questions