2012-02-22 32 views
1

這應該很簡單,但顯然我錯過了這個訣竅。我有一個POCO:如何序列化具有所需屬性名稱的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需要是不同的。我能做什麼?

回答

3

您需要修改與屬性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; } 
} 
+0

是否RestSharp行爲方式作爲框架的XmlSerializer的一樣嗎? – phoog 2012-02-22 22:09:58

+0

如果它不它可能應該;) – dice 2012-02-22 22:32:54

+0

謝謝,這沒有解決RestSharp問題,但我剛回到框架版本和您的建議工作。 – 2012-02-22 22:50:24

相關問題