如何使用所需的属性名称序列化POCO

时间:2012-02-22 21:31:57

标签: c# xml restsharp

这应该很简单,但显然我错过了诀窍。我有一个POCO:

public class job
{
    public string title { get; set; }
    public string company { get; set; }
    public string companywebsite { get; set; }

    public string[] locations { get; set; }
}

我使用RestSharp将其序列化为XML。我希望得到:

<job>
    <title>Hello title</title>
    <company>Hello company</company>
    <locations>New York</locations>
    <locations>Los Angeles</locations>
    <locations>Detroit</locations>
</job>

或理想情况......

<job>
    <title>Hello title</title>
    <company>Hello company</company>
    <locations>
         <location>New York</location>
         <location>Los Angeles</location>
         <location>Detroit</location>
    </locations>
</job>

但是我得到了这个:

<job>
    <title>Hello title</title>
    <company>Hello company</company>
    <locations>
         <String />
         <String />
         <String />
    </locations>
</job>

显然,POCO需要与众不同。我该怎么办?

1 个答案:

答案 0 :(得分:3)

您需要使用Attributes

修改XmlSerializer行为
public class job
{
    public string title { get; set; }
    public string company { get; set; }
    public string companywebsite { get; set; }
    [XmlArray("locations")]
    [XmlArrayItem("location")]
    public string[] locations { get; set; }
}