簡體   English   中英

自定義XML序列化添加額外屬性

[英]Custom XML Serialization adding extra attributes

我有以下課程。

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"}
};

我無法觸及ConfigurationItem類。 是否可以使用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();
}

仍在尋找更好的解決方案......

如果要輸出屬性,則需要一個將被序列化為屬性的屬性。 請嘗試以下方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM