RestSharp序列化没有正确序列化类名

时间:2013-03-22 15:38:38

标签: c# xml serialization restsharp

RestSharp似乎没有注意我用以下方法装饰我的一个类名的“SerializeAs”属性:

[Serializable, SerializeAs(Name = "person")]
public class Person
{
    [SerializeAs(Name = "first-name")]
    public string FirstName { get; set; }

    [SerializeAs(Name = "contact-data")]
    public ContactData ContactData { get; set; }
}

联系数据

public class ContactData
{
    [SerializeAs(Name = "email-addresses")]
    public List<EmailAddress> EmailAddresses { get; set; }
}

EmailAddress的

[SerializeAs(Name = "email-address")]
public class EmailAddress
{
    [SerializeAs(Name = "address")]
    public string Address { get; set; }

    [SerializeAs(Name = "location")]
    public string Location { get; set; }
}

我正在使用以下代码来序列化XML:

var request = new RestRequest("people/{id}.xml", Method.PUT);
request.AddParameter("id", person.Id, ParameterType.UrlSegment);
request.XmlSerializer = new XmlSerializer();
request.AddBody(person);

但是,生成的XML如下所示:

<person>
    <first-name>Scott</first-name> 
    <contact-data>
        <email-adresses>
            <EmailAddress>
                <address>my@email.com</address> 
                <location>Work</location> 
             </EmailAddress>
        </email-adresses>
    </contact-data>
</person>

您可以看到<EmailAddress>元素似乎忽略了SerializeAs属性,并且未按照我预期的那样序列化为“电子邮件地址”,而其他所有元素都工作得很好。有谁知道为什么会发生这种情况或如何解决它?

1 个答案:

答案 0 :(得分:4)

在浏览RestSharp源代码之后,我意识到这是RestSharp中内置的XmlSerializer中的一个错误。我已经修好并提交了pull request