本文介绍了使用字典成员和常规字段为类自定义xml序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个类作为DTO.为了灵活起见,我将其设计为DictionaryBase的子类.

I designed a class as a DTO. For flexible, I design it as the subclass of DictionaryBase.

[XmlRoot("dict")]
public abstract class DictionaryObject<TKey, TValue> : DictionaryBase, IDictionaryObject, IXmlSerializable
{
    public const string XML_INNER_DICT = "innerdict";
    public const string XML_KEY = "key";
    public const string XML_VALUE = "value";

    #region fields

    public string ExtString {get; set; }
    public object ExtObject {get; set; }

    #endregion

    #region indexes

    public virtual TValue this[TKey key]
    {
        get
        {
            return base.Dictionary[key];
        }
        set
        {
            base.Dictionary[key] = value;
        }
    }
    #endregion

    #region IXmlSerializable members
    public virtual XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }


    public override void ReadXml(XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof (TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof (TValue));
        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
        {
            return;
        }

        while (reader.NodeType != XmlNodeType.EndElement)
        {
            if (String.Compare(DictionaryObject.XML_INNER_DICT, reader.Name, true) == 0)
            {
                reader.ReadStartElement(DictionaryObject.XML_INNER_DICT);

                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    reader.ReadStartElement(DictionaryObject.XML_KEY);
                    TKey key = (TKey)keySerializer.Deserialize(reader);
                    reader.ReadEndElement();

                    reader.ReadStartElement(DictionaryObject.XML_VALUE);
                    TValue value = (TValue)valueSerializer.Deserialize(reader);
                    reader.ReadEndElement();

                    this.Set(key, value);
                    reader.MoveToContent();
                }

                reader.ReadEndElement();
            }
            else
            {
                // what should I do here, now I skip
                reader.Skip();
            }
        }
    }

    public override void WriteXml(XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        // what should I do here to serilize other fields

        if (this.Dictionary.Count > 0)
        {
            writer.WriteStartElement(DictionaryObject.XML_INNER_DICT);

            foreach (TKey key in this.Dictionary.Keys)
            {
                writer.WriteStartElement(DictionaryObject.XML_KEY);
                keySerializer.Serialize(writer, key);
                writer.WriteEndElement();

                writer.WriteStartElement(DictionaryObject.XML_VALUE);
                TValue value = this.Get(key);
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }

    }
    #endregion
}



我想为内部字典以及诸如ExtString,ExtObject之类的普通字段自定义xml序列化,我该怎么做才能完成工作.



I want to customize the xml serialization for the inner dictionary as well as normal fields like ExtString, ExtObject, how can I do to get the job done.

推荐答案


这篇关于使用字典成员和常规字段为类自定义xml序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:08
查看更多