0

I'm calling an API service that wraps all responses surrounded by a Response element:

<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns="http://www.example.com/stuff">
  <status upstairs="Normal" downstairs="Normal"/>
</Response>

My status class is:

class status {
    public string upstairs { get; set; }
    public string downstairs { get; set; }
}

I'm calling RestSharp to get the status (and prior to V106 this worked):

var client = new RestClient("http://www.example.com");
var request = new RestRequest("/api?get=status", Method.Get);
var response = client.Execute<status>(request);
if (response.Status.Equals(OK))
{
    var status = response.Data;
}

With the current version of RestSharp I see in response.Content that the data is there, however the deserializer failed with error

"<Response xmlns="http://www.example.com/stuff"> was not expected."

How do I specify that there is a parent element, but to ignore it?

1 Answer 1

0

Got it to work by changing my model to include the outer element, like so:

[XmlRoot("Response", Namespace="http://www.example.com/stuff")]
public class ResponseStatus {
    [XmlElement]
    public status status { get; set; }
}

public class status {
    [XmlAttribute]
    public string upstairs { get; set; }
    [XmlAttribute]
    public string downstairs { get; set; }
}

The code now looks like this:

var client = new RestClient("http://www.example.com");
var request = new RestRequest("/api?get=status", Method.Get);
var response = client.Execute<ResponseStatus>(request);
if (response.Status.Equals(OK)) {
    var status = response.Data.status;
}
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.