为什么 .NET 中没有 XML 可序列化字典? [英] Why isn't there an XML-serializable dictionary in .NET?

查看:25
本文介绍了为什么 .NET 中没有 XML 可序列化字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个 XML 可序列化的字典.实际上,我现在有两个完全不同的程序需要一个.我很惊讶地看到 .NET 没有.我在别处问了这个问题,得到了讽刺的回答.我不明白为什么这是一个愚蠢的问题.

I need an XML-serializable dictionary. Actually, I now have two quite different programs that need one. I was rather surprised to see that .NET doesn't have one. I asked the question elsewhere and got sarcastic responses. I don't understand why it's a stupid question.

鉴于各种 .NET 功能对 XML 序列化的依赖程度,有人可以启发我,为什么没有 XML 可序列化字典.希望您也能解释为什么有些人认为这是一个愚蠢的问题.我想我一定遗漏了一些基本的东西,我希望你能填补空白.

Can someone enlighten me, given how dependent various .NET features are on XML serialization, why there isn't an XML-serializable dictionary. Hopefully, you can also explain why some people consider that a daft question. I guess I must be missing something fundamental and I'm hoping you'll be able to fill in the gaps.

推荐答案

关于 XML 序列化的事情在于,它不仅仅是创建字节流.它还涉及创建一个 XML 模式,该字节流将根据该模式进行验证.XML Schema 中没有表示字典的好方法.你能做的最好的事情就是证明有一个唯一的键.

The thing about XML Serialization is that it's not just about creating a stream of bytes. It's also about creating an XML Schema that this stream of bytes would validate against. There's no good way in XML Schema to represent a dictionary. The best you could do is to show that there's a unique key.

您始终可以创建自己的包装器,例如 序列化字典的一种方法.

You can always create your own wrapper, for instance One Way to Serialize Dictionaries.

这篇关于为什么 .NET 中没有 XML 可序列化字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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