gpt4 book ai didi

c# - 使用 RestSharp 序列化列表中的对象

转载 作者:行者123 更新时间:2023-11-30 17:55:15 25 4
gpt4 key购买 nike

我在使用 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; }
}

电子邮件地址

[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>

我需要使用注释而不是类名本身对列表(或某些集合)中的 POCO 对象进行序列化。

有没有人找到解决方法?

最佳答案

将其追溯到 RestSharp 中的错误。我 fork 了 Git 存储库,修复了它并提交了 pull request .

关于c# - 使用 RestSharp 序列化列表中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15226860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com