2

I'm having some problem deserializeing a HttpResponseMessage into an object. The problem is that when the object should have deserialized all fields are null, no exceptions are thrown.

HttpContent content = new StringContent(xml);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
HttpResponseMessage response = await client.PostAsync("URL", content).ConfigureAwait(false);
// Parse response
if (response.IsSuccessStatusCode)
{
    XmlSerializer serializer = new XmlSerializer(typeof(ResponseObject));
    Stream responseStream = await response.Content.ReadAsStreamAsync();
    ResponseObject responseObject = serializer.Deserialize(responseStream) as ResponseObject;

    //Possible example of wrong data
    Console.WriteLine(responseObject.Message);
}

[XmlRoot("response")]
public class ResponseObject
{
    [XmlElement("session")]
    public string Session { get; set; }

    [XmlElement("status")]
    public string Status { get; set; }

    [XmlElement("message")]
    public string Message { get; set; }
}

Response message as a string

"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<response>
    <val n=\"session\">SESSION ID</val>
    <val n=\"status\">201</val>
    <val n=\"message\">Created</val>
</response>"

Have I missed something? I'm pretty new to serializing/deserializing. Grateful for pointers.

4
  • session,status and message are values of attribute n of tag val. Commented Aug 28, 2015 at 13:52
  • Does ResponseObject not require some collection of val elements in order to match the XML? Commented Aug 28, 2015 at 17:38
  • How stupid of me... That's what I get for trying to reuse code from an older project. I'll have a go at it on Monday when back at work. Commented Aug 29, 2015 at 14:45
  • Question: I have not had time to test anything yet but I have a question. I guess I should use a List<string> with XmlElement("val"). But how should I store the attribute for each xml row when it's beed serialized. I'd like to be able to find "session" in the list. Any hints? Commented Aug 30, 2015 at 15:47

1 Answer 1

2

Okay I solved it with the help of Eser and Biscuits in the comments.

I was trying to reuse code and did't really think about the response message having a different structure then the earlier project.

I changed my ResponseObject to this:

[XmlRoot("response")]
public abstract class ResponseObject
{
    [XmlIgnore]
    public bool Success { get; set; }

    [XmlIgnore]
    public string Session
    {
        get
        {
            var result = Values.FirstOrDefault(n => n.Name == "session");
            return result.Value;
        }
    }

    [XmlIgnore]
    public string Status
    {
        get
        {
            var result = Values.FirstOrDefault(n => n.Name == "status");
            return result.Value;
        }
    }

    [XmlIgnore]
    public string Message
    {
        get
        {
            var result = Values.FirstOrDefault(n => n.Name == "message");
            return result.Value;
        }
    }

    [XmlElement("val")]
    public List<ResponseXmlWrapper<string>> Values;
}

public class ResponseXmlWrapper<T>
{
    [XmlAttribute("n")]
    [JsonProperty("n")]
    public string Name { get; set; }

    [XmlText]
    [JsonProperty()]
    public T Value { get; set; }

    public ResponseXmlWrapper()
    {

    }

    public ResponseXmlWrapper(string attributeName, T value)
    {
        Name = attributeName;
        Value = value;
    }
}

I don't know if this is an optimal solution but it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.