2014-09-24 65 views
0

我有這樣的模式:如何序列化到「收藏與屬性」使用XML序列

[XmlArray(ElementName = "Listing")] 
[XmlArrayItem(ElementName = "Classification")] 
public List<Classification> classifications { get; set; } 

[XmlRoot("Listing")] 
public class Classification 
{ 
    [XmlAttribute("Name")] 
    public string name { get; set; } 

    [XmlText] 
    public string Value { get; set; } 
} 

這給了我這樣的:

<Listing> 
<Classification Name="Location">AsiaPacific</Classification> 
</Listing> 

我應該如何修改我的類來獲得這樣的:

<Listing reference = "MyReference"> 
<Classification Name="Location">AsiaPacific</Classification> 
</Listing> 

回答

1

數(數百個)試錯後,我得到了我需要通過修改模型的結果:

[XmlElement(ElementName = "Listing")] 
public ClassificationWrapper classificationWrapper { get; set; } 

public class ClassificationWrapper 
{ 
    [XmlAttribute("reference")] 
     public string ref= "MyReference"; 

     [XmlElement("Classification", typeof(Classification))] 
     public List<Classification> classifications { get; set; } 

public ClassificationWrapper() { this.classifications = new List<Classification>(); } 
} 
public class Classification 
{ 
     [XmlAttribute("Name")] 
     public string name { get; set; } 

     [XmlText] 
     public string Value { get; set; } 
}