22

この方法で IEnumerable をシリアル化できました。

[XmlArray("TRANSACTIONS")]
[XmlArrayItem("TRANSACTION", typeof(Record))]
public IEnumerable<BudgetRecord> Records
{
    get 
    {
        foreach(Record br in _budget)
        {
            yield return br;
        }
    }
}

しかし、今ではコレクションを含む辞書が必要であることに気付きましたDictionary<string, RecordCollection>(RecordCollection は IEnumerable を実装しています)。

どうすればそれを達成できますか?

4

6 に答える 6

15

次のブログ記事を見てください。

そしてこれ(英語ではありませんが、コードは便利です)


コード サンプル: http://web.archive.org/web/20100703052446/http://blogs.msdn.com/b/psheill/archive/2005/04/09/406823.aspx

using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System;
public static void Serialize(TextWriter writer, IDictionary dictionary)
{
    List<Entry> entries = new List<Entry>(dictionary.Count);
    foreach (object key in dictionary.Keys)
    {
        entries.Add(new Entry(key, dictionary[key]));
    }
    XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>));
    serializer.Serialize(writer, entries);
}
public static void Deserialize(TextReader reader, IDictionary dictionary)
{
    dictionary.Clear();
    XmlSerializer serializer = new XmlSerializer(typeof(List<Entry>));
    List<Entry> list = (List<Entry>)serializer.Deserialize(reader);
    foreach (Entry entry in list)
    {
        dictionary[entry.Key] = entry.Value;
    }
}
public class Entry
{
    public object Key;
    public object Value;
    public Entry()
    {
    }

    public Entry(object key, object value)
    {
        Key = key;
        Value = value;
    }
}

キーと値が文字列の場合、次のような出力が生成されます。

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEntry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Entry>
    <Key xsi:type="xsd:string">MyKey</Key>
    <Value xsi:type="xsd:string">MyValue</Value>  
  </Entry>
  <Entry>    
    <Key xsi:type="xsd:string">MyOtherKey</Key>    
    <Value xsi:type="xsd:string">MyOtherValue</Value>  
  </Entry>
</ArrayOfEntry>
于 2010-09-08T19:52:11.477 に答える
12

この別の簡単な方法を試してください:

    void Main()
    {
        var model = new Foo() 
        { 
            RealDictionary = new Dictionary<string, int> { {"A", 23}, {"B", 40} } 
        };
        model.RealDictionary.Add("C", 69);
        
        using (var writer = XmlWriter.Create("c:\\test1.xml"))
            (new XmlSerializer(typeof(Foo))).Serialize(writer, model);
    }
    
    [Serializable]
    public class Foo
    {
        [XmlArray(ElementName ="Elements")]
        [XmlArrayItem(ElementName = "Element")]
        public List<KeyValueElement> SerializableDictionary 
        { 
            get { return RealDictionary.Select(x => new KeyValueElement {Key = x.Key, Value = x.Value}).ToList(); } 
            set { RealDictionary = value.ToDictionary(x=> x.Key, x => x.Value); }
        }
    
        [XmlIgnore]
        public Dictionary<string, int> RealDictionary {get;set;}  
    }
    
    [Serializable]
    public class KeyValueElement
    {
        public string Key { get; set; }
        public int Value { get; set; }  
    }

xml の結果は次のとおりです。

    <Foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <Elements>
            <Element>
                <Key>A</Key><Value>23</Value>
            </Element>
            <Element>
                <Key>B</Key><Value>40</Value>
            </Element>
            <Element>
                <Key>C</Key><Value>69</Value>
            </Element>
        </Elements>
    </Foo>
于 2013-11-21T10:41:22.340 に答える
10

しばらくの間、以下を使用しました。元々はここから。

namespace SerializeDictionary
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    /// <summary>
    /// Represents an XML serializable collection of keys and values.
    /// </summary>
    /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
    /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
    [Serializable]
    [XmlRoot("dictionary")]
    public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
    {
        /// <summary>
        /// The default XML tag name for an item.
        /// </summary>
        private const string DefaultItemTag = "item";

        /// <summary>
        /// The default XML tag name for a key.
        /// </summary>
        private const string DefaultKeyTag = "key";

        /// <summary>
        /// The default XML tag name for a value.
        /// </summary>
        private const string DefaultValueTag = "value";

        /// <summary>
        /// The XML serializer for the key type.
        /// </summary>
        private static readonly XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));

        /// <summary>
        /// The XML serializer for the value type.
        /// </summary>
        private static readonly XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="SerializableDictionary&lt;TKey, TValue&gt;"/> class.
        /// </summary>
        public SerializableDictionary()
        {
        }

        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="SerializableDictionary&lt;TKey, TValue&gt;"/> class.
        /// </summary>
        /// <param name="info">A
        /// <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object
        /// containing the information required to serialize the
        /// <see cref="T:System.Collections.Generic.Dictionary`2"/>.
        /// </param>
        /// <param name="context">A
        /// <see cref="T:System.Runtime.Serialization.StreamingContext"/> structure
        /// containing the source and destination of the serialized stream
        /// associated with the
        /// <see cref="T:System.Collections.Generic.Dictionary`2"/>.
        /// </param>
        protected SerializableDictionary(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        /// <summary>
        /// Gets the XML tag name for an item.
        /// </summary>
        protected virtual string ItemTagName
        {
            get
            {
                return DefaultItemTag;
            }
        }

        /// <summary>
        /// Gets the XML tag name for a key.
        /// </summary>
        protected virtual string KeyTagName
        {
            get
            {
                return DefaultKeyTag;
            }
        }

        /// <summary>
        /// Gets the XML tag name for a value.
        /// </summary>
        protected virtual string ValueTagName
        {
            get
            {
                return DefaultValueTag;
            }
        }

        /// <summary>
        /// Gets the XML schema for the XML serialization.
        /// </summary>
        /// <returns>An XML schema for the serialized object.</returns>
        public XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Deserializes the object from XML.
        /// </summary>
        /// <param name="reader">The XML representation of the object.</param>
        public void ReadXml(XmlReader reader)
        {
            var wasEmpty = reader.IsEmptyElement;

            reader.Read();
            if (wasEmpty)
            {
                return;
            }

            try
            {
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    this.ReadItem(reader);
                    reader.MoveToContent();
                }
            }
            finally
            {
                reader.ReadEndElement();
            }
        }

        /// <summary>
        /// Serializes this instance to XML.
        /// </summary>
        /// <param name="writer">The XML writer to serialize to.</param>
        public void WriteXml(XmlWriter writer)
        {
            foreach (var keyValuePair in this)
            {
                this.WriteItem(writer, keyValuePair);
            }
        }

        /// <summary>
        /// Deserializes the dictionary item.
        /// </summary>
        /// <param name="reader">The XML representation of the object.</param>
        private void ReadItem(XmlReader reader)
        {
            reader.ReadStartElement(this.ItemTagName);
            try
            {
                this.Add(this.ReadKey(reader), this.ReadValue(reader));
            }
            finally
            {
                reader.ReadEndElement();
            }
        }

        /// <summary>
        /// Deserializes the dictionary item's key.
        /// </summary>
        /// <param name="reader">The XML representation of the object.</param>
        /// <returns>The dictionary item's key.</returns>
        private TKey ReadKey(XmlReader reader)
        {
            reader.ReadStartElement(this.KeyTagName);
            try
            {
                return (TKey)keySerializer.Deserialize(reader);
            }
            finally
            {
                reader.ReadEndElement();
            }
        }

        /// <summary>
        /// Deserializes the dictionary item's value.
        /// </summary>
        /// <param name="reader">The XML representation of the object.</param>
        /// <returns>The dictionary item's value.</returns>
        private TValue ReadValue(XmlReader reader)
        {
            reader.ReadStartElement(this.ValueTagName);
            try
            {
                return (TValue)valueSerializer.Deserialize(reader);
            }
            finally
            {
                reader.ReadEndElement();
            }
        }

        /// <summary>
        /// Serializes the dictionary item.
        /// </summary>
        /// <param name="writer">The XML writer to serialize to.</param>
        /// <param name="keyValuePair">The key/value pair.</param>
        private void WriteItem(XmlWriter writer, KeyValuePair<TKey, TValue> keyValuePair)
        {
            writer.WriteStartElement(this.ItemTagName);
            try
            {
                this.WriteKey(writer, keyValuePair.Key);
                this.WriteValue(writer, keyValuePair.Value);
            }
            finally
            {
                writer.WriteEndElement();
            }
        }

        /// <summary>
        /// Serializes the dictionary item's key.
        /// </summary>
        /// <param name="writer">The XML writer to serialize to.</param>
        /// <param name="key">The dictionary item's key.</param>
        private void WriteKey(XmlWriter writer, TKey key)
        {
            writer.WriteStartElement(this.KeyTagName);
            try
            {
                keySerializer.Serialize(writer, key);
            }
            finally
            {
                writer.WriteEndElement();
            }
        }

        /// <summary>
        /// Serializes the dictionary item's value.
        /// </summary>
        /// <param name="writer">The XML writer to serialize to.</param>
        /// <param name="value">The dictionary item's value.</param>
        private void WriteValue(XmlWriter writer, TValue value)
        {
            writer.WriteStartElement(this.ValueTagName);
            try
            {
                valueSerializer.Serialize(writer, value);
            }
            finally
            {
                writer.WriteEndElement();
            }
        }
    }
}
于 2010-09-08T21:31:29.513 に答える
6

Gildorsの回答に基づくさらに短いバージョンを次に示します。

[XmlElement("Dictionary")]
public List<KeyValuePair<string, string>> XMLDictionaryProxy
{
    get
    {
        return new List<KeyValuePair<string, string>>(this.Dictionary);
    }
    set
    {
        this.Dictionary = new Dictionary<string, string>();
        foreach (var pair in value)
            this.Dictionary[pair.Key] = pair.Value;
    }
}

[XmlIgnore]
public Dictionary<string, string> Dictionary
{
    get; set;
}

楽しみ。

于 2014-03-03T10:35:23.313 に答える
0

このSerializableDictionaryクラスは通常のように使用Dictionaryでき、Serialize()

要旨

SerializableDictionary.cs

using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

/// <summary>
/// Base on https://weblogs.asp.net/pwelter34/444961
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    // XmlSerializer.Deserialize() will create a new Object, and then call ReadXml()
    // So cannot use instance field, use class field.

    public static string itemTag = "item";
    public static string keyTag = "key";
    public static string valueTag = "value";

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        if (reader.IsEmptyElement)
            return;

        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        reader.ReadStartElement();

        // IsStartElement() will call MoveToContent()
        // reader.MoveToContent();
        while (reader.IsStartElement(itemTag))
        {
            reader.ReadStartElement(itemTag);

            reader.ReadStartElement(keyTag);
            TKey key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();

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

            reader.ReadEndElement();
            this.Add(key, value);

            // IsStartElement() will call MoveToContent()
            // reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

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

        foreach (var kvp in this)
        {
            writer.WriteStartElement(itemTag);

            writer.WriteStartElement(keyTag);
            keySerializer.Serialize(writer, kvp.Key);
            writer.WriteEndElement();

            writer.WriteStartElement(valueTag);
            valueSerializer.Serialize(writer, kvp.Value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }
}

Demo.cs

using System;
using System.IO;
using System.Xml.Serialization;

//[XmlRoot("AnimalDictionary")]
public class SDict : SerializableDictionary<string, string>
{
}

namespace Test
{
    public class Program
    {

        static void Main(string[] args)
        {
            var dict = new SDict();
            //SDict.itemTag = "AnimalCount";
            //SDict.keyTag = "Type";
            //SDict.valueTag = "Count";
            dict.Add("Cat", "1");
            dict.Add("Dog", "2");

            foreach (var kvp in dict)
                Console.WriteLine(kvp);

            XmlSerializer serializer = new XmlSerializer(typeof(SDict));

            using (var writer = new StreamWriter("dict.xml"))
            {
                serializer.Serialize(writer, dict);
            }

            Console.WriteLine("\nXML File:");
            Console.WriteLine(File.ReadAllText("dict.xml"));

            using (var reader = new StreamReader("dict.xml"))
            {
                dict = serializer.Deserialize(reader) as SDict;
            }

            Console.WriteLine("\nAfter Deserialize");

            foreach (var kvp in dict)
                Console.WriteLine(kvp);

            Console.ReadLine();
        }
    }
}
于 2021-04-29T07:21:02.577 に答える