2

I am posting a get request and it returns a xml file, but when I try to deserialize it into a list I get the following error :

{"No parameterless constructor defined for this object."}

RestClient class (calling GetResourceList):

public T Execute<T>(RestRequest request) where T : new()
        {
            var client = new RestClient();
            client.BaseUrl = new Uri(m_URL);
            client.Authenticator = new HttpBasicAuthenticator(m_Username, m_Password);

            var response = client.Execute<T>(request);

            if (response.ErrorException != null)
            {
                const string message = "Error retrieving response.  Check inner details for more info.";
                var exception = new ApplicationException(message, response.ErrorException);
                throw exception;
            }
            return response.Data;
        }

        public List<resource> GetResourceList()
        {
            var request = new RestRequest();
            request.Resource = "resource";
            request.AddHeader("Accept", "application/xml");

            return Execute<List<resource>>(request);
        }

Model (generated with xsd from a xsd file provided by the API):

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class resource {

        private string selfField;

        private string userIDField;

        private string firstNameField;

        private string lastNameField;

        private string extensionField;

        private nameUriPair resourceGroupField;

        private skillCompetency[] skillMapField;

        private bool autoAvailableField;

        private int typeField;

        private nameUriPair teamField;

        private resourcePrimarySupervisorOf primarySupervisorOfField;

        private resourceSecondarySupervisorOf secondarySupervisorOfField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string self {
            get {
                return this.selfField;
            }
            set {
                this.selfField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string userID {
            get {
                return this.userIDField;
            }
            set {
                this.userIDField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string firstName {
            get {
                return this.firstNameField;
            }
            set {
                this.firstNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string lastName {
            get {
                return this.lastNameField;
            }
            set {
                this.lastNameField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string extension {
            get {
                return this.extensionField;
            }
            set {
                this.extensionField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public nameUriPair resourceGroup {
            get {
                return this.resourceGroupField;
            }
            set {
                this.resourceGroupField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlArrayItemAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
        public skillCompetency[] skillMap {
            get {
                return this.skillMapField;
            }
            set {
                this.skillMapField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public bool autoAvailable {
            get {
                return this.autoAvailableField;
            }
            set {
                this.autoAvailableField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public int type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public nameUriPair team {
            get {
                return this.teamField;
            }
            set {
                this.teamField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public resourcePrimarySupervisorOf primarySupervisorOf {
            get {
                return this.primarySupervisorOfField;
            }
            set {
                this.primarySupervisorOfField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public resourceSecondarySupervisorOf secondarySupervisorOf {
            get {
                return this.secondarySupervisorOfField;
            }
            set {
                this.secondarySupervisorOfField = value;
            }
        }
    }

Return XML :

<resources>
    <resource>...</resource>
    <resource>...</resource>
    <resource>...</resource>
</resources>

Adding a empty constructor in the resource class / supporting classes doesn't seem to help. Any idea's ? Also tried deserializing a resource directly instead of the whole list same error.

2
  • I tried generating a XSD scheme from the response and then using that to generate the classes with xsd.exe ... /classes but then it doens't throw the exception but just returns null. Commented Dec 3, 2015 at 12:09
  • I just got the same error and same scenario. It has something to do with the way that XSD generates the class because when using a VS Plugin XSD to Code then it works just fine. I'll keep investigating. Commented May 27, 2016 at 5:53

2 Answers 2

6

After running in to the same issue, I found that RestSharp doesn't play nice with the classes created using xsd.exe.

Reverted back to the classic deserializer:

System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));

using (StringReader sr = new StringReader(response.Content))
    return (T)ser.Deserialize(sr);
1

Regarding to GitHub you can not deserialize Arrays. You have to use List instead. (https://github.com/restsharp/RestSharp/issues/547)

So try this one:

private List<skillCompetency> skillMapField;

instead of:

private skillCompetency[] skillMapField;
1
  • "According" to github :) Commented Mar 25, 2021 at 2:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.