没有父节点的C#序列化字典参数 [英] c# serialize dictionary parameter without parent node

查看:74
本文介绍了没有父节点的C#序列化字典参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,希望将其序列化为xml. 该类包含一个字典..将其切换为可序列化的版本(使用writexml/readxml).

i have a class and wish to serialize it into xml. the class contains a dictionary.. switched it to a serializeable version (with writexml / readxml).

问题在于字典参数被序列化时..它用父元素"Attributes"包装字典元素,而我不希望那样.

the problem is that when the dictionary parameter gets serialized.. it wraps the dictionary elements with a parent element "Attributes" and i dont want that.

示例:

public class Product
{
    public String Identifier{ get; set; }
    [XmlElement]
    public SerializableDictionary<string,string> Attributes { get; set; } //custom serializer
}

将这个Product类放入List结构中,然后将整个序列序列化,结果为:

This Product class gets put in a List structure, then the whole thing serialized, resulting to:

<Products>
<Product>
     <Identifier>12345</Identifier>
     <Attributes>
          <key1> value 1</key1>
          <key2> value 2</key2>
     </Attributes>
</Product>
</Products>

Id喜欢没有节点包装.

Id like to not have the node wrapper.

我使用的是序列化字典类,但是通过它的WriteXml im只能影响键值对.而不是父元素.

Im using a Serialized Dictionary class that goes around, but through its WriteXml im only able to influence the key value pairs.. not the parent element.

任何我可以插入以说linqpad的自给自足的例子都很好. 这是可序列化字典的简短版本.

Any self sufficient examples that i could plug to say linqpad would be great.. Heres a short version of the serializable dictionary..

   [XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    #region IXmlSerializable Members
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        //not including for the sake of brevity
    }
    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement(key.ToString());
            TValue value = this[key];
            if (value == null)
                writer.WriteValue(String.Empty); //render empty ones.
            else
                writer.WriteValue(value.ToString());
            writer.WriteEndElement();
        }
    }
    #endregion
}

推荐答案

如果不希望使用<Attributes>元素,则需要将WriteXml方法移动到Product -class.

If you don't want the <Attributes> element, you need to move the WriteXml method to the Product-class.

public void WriteXml(XmlWriter writer)
{
    writer.WriteElementString("Identifier", Identifier.ToString("d"));
    XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
    foreach (TKey key in Attributes.Keys)
    {
        writer.WriteStartElement(key.ToString());
        TValue value = Attributes[key];
        if (value == null)
            writer.WriteValue(String.Empty); //render empty ones.
        else
            writer.WriteValue(value.ToString());
        writer.WriteEndElement();
    }
}

这篇关于没有父节点的C#序列化字典参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆