0

i've an xml from some api :

<auditypes>
     <auditype code="a" description="aaa"/>
     <auditype code="b" description="bbb"/>
     <auditype code="c" description="ccc"/>
     <auditype code="d" description="ddd"/>
     <auditype code="e" description="eee"/>
</auditypes>

and mapping as object in C# class :

    public class auditypes
    {
        public List<auditype> auditype { get; set; }
    }

    public class auditype
    {
        public string code { get; set; }
        public string description { get; set; }
    }

I call it with this function :

    public List<auditypes> Execute<auditypes>(RestRequest request) where auditypes : new()
    {
        var client = new RestClient();
        client.BaseUrl = "https://www.myurl.com/auditypes";
        var response = client.Execute<auditypes>(request);

        return response.Data as List<auditypes>;
    }

    public List<auditypes> GetCall()
    {
        var request = new RestRequest();
        request.RequestFormat = DataFormat.Xml;
        request.RootElement = "auditype";
        return Execute<auditypes>(request);
    }

but it always return null, does anyone knows why is this happening?

2 Answers 2

3

The generic parameter passed to Execute<T> is the type that should be deserialized by the RestSharp library. That means that your response.Data property is already of type T, which in your case is auditypes. But when you return you try to cast it to a List<auditypes> where no such cast exists.

Also, there is no need for the type constraint, as your method is not generic as it accepts an explicit type.

Refactor your method:

public auditypes Execute<auditypes>(RestRequest request)
{
    var client = new RestClient();
    client.BaseUrl = "https://www.myurl.com/auditypes";
    var response = client.Execute<auditypes>(request);

    return response.Data;
}
Sign up to request clarification or add additional context in comments.

7 Comments

it still not return the xml data, but it return length=0 not retun null
Are you sure you are receiving a List<auditypes> abd not just auditypes?
yes i received that, the return type is List<auditypes> but Data count = 0
Is the XML you posted a full one? Or are there more auditypes attributes?
I got an error when try it(but forgot what is exactly). The problem isnt in my auditypes function btw, i found it in my answer. Thanks :)
|
0

Finally, it work for me :)

public auditypes Execute<auditypes>(RestRequest request) where auditypes : new()
{
    var client = new RestClient();
    client.BaseUrl = "https://www.myurl.com/auditypes";
    var response = client.Execute<auditypes>(request).Data;

    return response;
}

public auditypes GetCall()
{
    var request = new RestRequest();
    request.RequestFormat = DataFormat.Xml;
    return Execute<auditypes>(request);
}

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.