为什么 XmlSerializer 不支持字典?

发布于 2024-09-03 04:28:03 字数 335 浏览 5 评论 0 原文

只是好奇为什么 XmlSerializer 不支持字典?

您可以通过使用 DataContractSerializer 并将对象写入 XmlTextWriter 来轻松解决这个问题,但是字典的哪些特征使得 XmlSerializer 变得困难 考虑到它实际上是一个 KeyValuePairs 数组。

事实上,您可以将 IDictionary 传递给需要 IEnumerable> 的方法。

Just curious as to why Dictionary is not supported by XmlSerializer?

You can get around it easily enough by using DataContractSerializer and writing the object to a XmlTextWriter, but what are the characteristics of a Dictionary that makes it difficult for a XmlSerializer to deal with considering it's really an array of KeyValuePairs.

In fact, you can pass an IDictionary<TKey, TItem> to a method expecting an IEnumerable<KeyValuePairs<TKey, ITem>>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伪装你 2024-09-10 04:28:03

哈希表通常需要哈希码和相等比较器提供程序。这些不能轻松地在 XML 中序列化,并且绝对不可移植。

但我想你已经找到了答案。只需将哈希表序列化为 List>,然后(重新)将其构建为哈希表即可。

Hashtables need hashcode and equality comparer providers generally. These cant be serialized easily in XML, and definitely will not be portable.

But I think you already found your answer. Just serialize the hashtable as a List<KeyValuePair<K,V>> and then (re)construct it into a hashtable.

淑女气质 2024-09-10 04:28:03

这已经太晚了 - 但我在自己寻找答案时发现了这个问题,并认为我会分享我的最终答案,即用一个可以序列化所有内容的不同工具替换 XmlSerializer

http://www.sharpserializer.com

它开箱即用,可以序列化字典和多层自定义类型,甚至使用接口作为类型参数的泛型。还拥有完全许可的许可证。

谢谢帕维尔·伊兹科夫斯基!

This is waaay late - but I found this question whilst looking for the answer myself, and thought I'd share my eventual answer which was to replace XmlSerializer with a different tool that will serialize everything:

http://www.sharpserializer.com

It worked for me straight out of the box, serializing Dictionaries, and multi-layered custom types, and even generics using interfaces as type arguments. Also has a fully permissive license.

Thank you Pawel Idzikowski!

柠栀 2024-09-10 04:28:03

您可以使用 ExtendedXmlSerializer
如果您有一个类:

public class TestClass
{
    public Dictionary<int, string> Dictionary { get; set; }
}

并创建此类的实例:

var obj = new TestClass
{
    Dictionary = new Dictionary<int, string>
    {
        {1, "First"},
        {2, "Second"},
        {3, "Other"},
    }
};

您可以使用 ExtendedXmlSerializer 序列化此对象:

var serializer = new ConfigurationContainer()
    .UseOptimizedNamespaces() //If you want to have all namespaces in root element
    .Create();

var xml = serializer.Serialize(
    new XmlWriterSettings { Indent = true }, //If you want to formated xml
    obj);

输出 xml 将如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:sys="https://extendedxmlserializer.github.io/system" xmlns:exs="https://extendedxmlserializer.github.io/v2" xmlns="clr-namespace:ExtendedXmlSerializer.Samples;assembly=ExtendedXmlSerializer.Samples">
  <Dictionary>
    <sys:Item>
      <Key>1</Key>
      <Value>First</Value>
    </sys:Item>
    <sys:Item>
      <Key>2</Key>
      <Value>Second</Value>
    </sys:Item>
    <sys:Item>
      <Key>3</Key>
      <Value>Other</Value>
    </sys:Item>
  </Dictionary>
</TestClass>

您可以从 nuget 或运行以下命令:

Install-Package ExtendedXmlSerializer

You can use ExtendedXmlSerializer.
If you have a class:

public class TestClass
{
    public Dictionary<int, string> Dictionary { get; set; }
}

and create instance of this class:

var obj = new TestClass
{
    Dictionary = new Dictionary<int, string>
    {
        {1, "First"},
        {2, "Second"},
        {3, "Other"},
    }
};

You can serialize this object using ExtendedXmlSerializer:

var serializer = new ConfigurationContainer()
    .UseOptimizedNamespaces() //If you want to have all namespaces in root element
    .Create();

var xml = serializer.Serialize(
    new XmlWriterSettings { Indent = true }, //If you want to formated xml
    obj);

Output xml will look like:

<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:sys="https://extendedxmlserializer.github.io/system" xmlns:exs="https://extendedxmlserializer.github.io/v2" xmlns="clr-namespace:ExtendedXmlSerializer.Samples;assembly=ExtendedXmlSerializer.Samples">
  <Dictionary>
    <sys:Item>
      <Key>1</Key>
      <Value>First</Value>
    </sys:Item>
    <sys:Item>
      <Key>2</Key>
      <Value>Second</Value>
    </sys:Item>
    <sys:Item>
      <Key>3</Key>
      <Value>Other</Value>
    </sys:Item>
  </Dictionary>
</TestClass>

You can install ExtendedXmlSerializer from nuget or run the following command:

Install-Package ExtendedXmlSerializer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文