2013-04-24 99 views
4

我有以下類。添加額外屬性的自定義XML序列化

public class ConfigurationItem 
{ 
    public String Type { get; set; } 
    public String Value { get; set; } 
} 

該代碼執行序列化。

static void Main(string[] args) 
{ 
    List<ConfigurationItem> cis = new List<ConfigurationItem>(); 
    cis.Add(new ConfigurationItem() { Type = "Car", Value = "Car Value" }); 
    cis.Add(new ConfigurationItem() { Type = "Bike", Value = "Bike Value" }); 

    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(cis.GetType()); 
    x.Serialize(Console.Out, cis); 
} 

實際輸出如下。

<?xml version="1.0" encoding="IBM437"?> 
<ArrayOfConfigurationItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <ConfigurationItem> 
     <Type>Car</Type> 
     <Value>Car Value</Value> 
    </ConfigurationItem> 
    <ConfigurationItem> 
     <Type>Bike</Type> 
     <Value>Bike Value</Value>   
    </ConfigurationItem> 
</ArrayOfConfigurationItem> 

我想製作下面的XML。

<?xml version="1.0" encoding="IBM437"?> 
<ArrayOfConfigurationItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <ConfigurationItem> 
     <Type>Car</Type> 
     <Value Label="Car Label">Car Value</Value> 
    </ConfigurationItem> 
    <ConfigurationItem> 
     <Type>Bike</Type> 
     <Value Label="Bike Label">Bike Value</Value>   
    </ConfigurationItem> 
</ArrayOfConfigurationItem> 

我有以下類型標籤可用

Dictionary<String, String> ValueLabels = new Dictionary<string, string>() 
{ 
    {"Car","Car Label"}, 
    {"Bike","Bike Label"} 
}; 

我不能碰形態項目類映射表。有沒有可能使用System.Xml.Serialization.XmlAttributeOverrides或類似的東西?

編輯1 我有一個醜陋的解決方案,我現在正在使用。我正在使用正常序列化並手動添加數據到XmlDocument。

static void Main(string[] args) 
{ 
    List<ConfigurationItem> cis = new List<ConfigurationItem>(); 
    cis.Add(new ConfigurationItem(){Type = "Car", Value = "Car Value"}); 
    cis.Add(new ConfigurationItem(){Type = "Bike", Value = "Bike Value"}); 

    Dictionary<String, String> valueLabels = new Dictionary<string, string>() 
    { 
     {"Car","Car Label"}, 
     {"Bike","Bike Label"} 
    }; 

    var detailDocument = new System.Xml.XmlDocument(); 
    var nav = detailDocument.CreateNavigator(); 

    if (nav != null) 
    { 
     using (System.Xml.XmlWriter w = nav.AppendChild()) 
     { 
      var ser = new System.Xml.Serialization.XmlSerializer(cis.GetType()); 
      ser.Serialize(w, cis); 
     } 
    } 
    var nodeList = detailDocument.DocumentElement.SelectNodes("//ConfigurationItem"); 
    foreach (System.Xml.XmlNode node in nodeList) 
    { 
     String type = ((System.Xml.XmlElement)node.SelectNodes("Type")[0]).InnerText; 
     ((System.Xml.XmlElement)node.SelectNodes("Value")[0]).SetAttribute("Label", valueLabels[type]); 
    } 

    System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Console.Out); 
    writer.Formatting = System.Xml.Formatting.Indented; 
    detailDocument.WriteTo(writer);    

    Console.ReadLine(); 
} 

還在尋找更好的解決辦法...

+0

怎麼樣做這樣的事情http://stackoverflow.com/a/1012422/299327? – 2013-06-13 19:16:32

+0

我編輯過你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 – 2013-06-13 19:26:22

回答

2

如果你想輸出的屬性,你需要將被序列化爲屬性的屬性。請嘗試以下操作:

public class ConfigurationItem 
{ 
    public String Type { get; set; } 
    public String Value { get; set; } 
    [XmlAttribute("Label")] 
    public string Label 
    { 
     get {return Value;} 
     set {Value = value;} 
    } 
} 
+0

感謝您的回答。唯一的問題是我無法觸摸ConfigurationItem類並在其中添加額外的屬性。 – ahagman 2013-06-17 19:24:31

+0

在這種情況下,你幾乎不走運,除非你爲自己創建了一個單獨的類,僅用於序列化。 – 2013-06-18 06:57:39