【问题标题】:Restsharp XML DeserializationRestsharp XML反序列化
【发布时间】:2015-11-27 12:14:03
【问题描述】:

我正在发布一个 get 请求,它返回一个 xml 文件,但是当我尝试将其反序列化为列表时,我收到以下错误:

{"没有为此对象定义无参数构造函数。"}

RestClient 类(调用 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);
        }

模型(使用 API 提供的 xsd 文件中的 xsd 生成):

/// <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;
            }
        }
    }

返回 XML:

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

在资源类/支持类中添加一个空的构造函数似乎没有帮助。有任何想法吗 ?还尝试直接反序列化资源而不是整个列表同样的错误。

【问题讨论】:

  • 我尝试从响应中生成一个 XSD 方案,然后使用它来生成带有 xsd.exe ... /classes 的类,但它不会抛出异常,而只是返回 null。跨度>
  • 我刚刚遇到了同样的错误和同样的情况。它与 XSD 生成类的方式有关,因为当使用 VS Plugin XSD to Code 时,它​​工作得很好。我会继续调查。

标签: c# xml xsd deserialization restsharp


【解决方案1】:

在遇到同样的问题后,我发现 RestSharp 与使用 xsd.exe 创建的类不兼容。

恢复到经典的反序列化器:

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

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

【讨论】:

    【解决方案2】:

    关于 GitHub,您不能反序列化数组。您必须改用 List 。 (https://github.com/restsharp/RestSharp/issues/547)

    所以试试这个:

    private List<skillCompetency> skillMapField;
    

    代替:

    private skillCompetency[] skillMapField;
    

    【讨论】:

    • “根据”github :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多