使用RestSharp将我的POCO对象序列化为XML时出现问题。我的POCO对象如下所示:
人
[SerializeAs(Name = "person")]
public class Person
{
[SerializeAs(Name = "author-id")]
public int? AuthorId { get; set; }
[SerializeAs(Name = "background")]
public string Background { get; set; }
[SerializeAs(Name = "first-name")]
public string FirstName { get; set; }
[SerializeAs(Name = "last-name")]
public string LastName { get; set; }
[SerializeAs(Name = "id")]
public int? Id { get; set; }
[SerializeAs(Name = "company-name")]
public string CompanyName { get; set; }
[SerializeAs(Name = "title")]
public string Title { get; set; }
[SerializeAs(Name = "contact-data")]
public ContactData ContactData { get; set; }
[SerializeAs(Name = "tags")]
public List<tag> Tags { get; set; }
}
联系数据
public class ContactData
{
[SerializeAs(Name = "addresses")]
public List<object> Addresses { get; set; }
[SerializeAs(Name = "phone-numbers")]
public List<object> PhoneNumbers { get; set; }
[SerializeAs(Name = "email-adresses")]
public List<object> EmailAddresses { get; set; }
[SerializeAs(Name = "web-addresses")]
public List<object> WebAddresses { get; set; }
}
EmailAddress的
[SerializeAs(Name = "email-address")]
public class EmailAddress
{
[SerializeAs(Name = "address")]
public string Address { get; set; }
[SerializeAs(Name = "id")]
public int? Id { get; set; }
[SerializeAs(Name = "location")]
public string Location { get; set; }
}
这在序列化时给了我以下XML:
<person>
<first-name>my firstname</first-name>
<contact-data>
<email-adresses>
<EmailAddress>
<address>my@email.com</address>
<location>Work</location>
</EmailAddress>
</email-adresses>
</contact-data>
<tags>
<tag>
<name>Nyhedsbrev</name>
</tag>
</tags>
</person>
您可能会注意到,EmailAddress SerializeAs
会被忽略。我认为这可能是因为它位于List
,因为Person
对象被正确序列化为<person>
我需要使用注释而不是类名本身来序列化List(或某些集合)中的POCO对象。
有没有人找到解决方法?