1

这应该很简单,但显然我错过了诀窍。我有一个 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 需要有所不同。我能做些什么?

4

1 回答 1

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; }
}
于 2012-02-22T21:43:12.713 回答