如何用所需的属性名称序列化POCO
本文关键字:序列化 POCO 属性 何用所 | 更新日期: 2023-09-27 18:25:24
这应该很简单,但显然我错过了技巧。我有一个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需要有所不同。我能做什么?
您需要使用属性修改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; }
}