Why can't a dictionary object be XmlSerialized in C #?

Serialization seems to be very simple. Assuming the key and value are serializable, what could be simpler than representing key-value pairs in XML ?!

To all commentators: First of all, I appreciate your answers, but ... I'm less interested in labor relations (there really are a lot of SerializableDictionary implementations on the Internet), as well as regarding the reasons for this design decision.

+3
source share
6 answers

This guy created an XML serializable dictionary .

+4
source

"Why" part:

, XmlSerializer , IDictionary, IDictionary . - , IDictionary . XmlSerializer . , IXmlSerializable , /.

XmlSerializer, , XmlReader XmlWriter XML. , , IXmlSerializable, , IXmlSerializable. , IXmlSerializable, .

: XML:

+4

, ?

XML . / XML, XML-, XML.

XML-? , ? , , ?


​​ , . , , , , , , .

, , DataContractSerializer XML-, - WCF XML-. , , , .

+3

, (), XML, : DataContractSerializer:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;//you need to set a reference to System.Runtime.Serialization.dll!

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var dictionary = new Dictionary<int, string>();
            dictionary.Add(1,"John");
            dictionary.Add(2,"Jane");

            var serializer = new DataContractSerializer(typeof(Dictionary<int, string>));
            using (var stream = new FileStream("dictionary.xml", FileMode.Create, FileAccess.Write))
            {
                serializer.WriteObject(stream,dictionary);
            }

            var xml = File.ReadAllText("dictionary.xml");

            Console.WriteLine("dictionary was stored as: {0}",xml);
            Console.ReadLine();
        }
    }
}
+1
0

DataContract Serialization WCF. .

ISerializable , opt-in, ISerializable .

, DataContract.

0
source

Source: https://habr.com/ru/post/1720209/


All Articles