Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
C# 如何从xml中反序列化出字典(Dictionary)_创想鸟

C# 如何从xml中反序列化出字典(Dictionary)

C#中XML反序列化不直接支持Dictionary,需通过封装KeyValueItem列表实现。1. 定义Key、Value属性的KeyValueItem类;2. 创建包含List的SerializableDictionary类,并提供ToDictionary方法;3. XML结构需为……格式;4. 使用XmlSerializer反序列化后转换为Dictionary。此方法适用于简单键值场景,复杂类型可扩展或改用JSON方案。

c# 如何从xml中反序列化出字典(dictionary)

在 C# 中,XML 反序列化默认不直接支持 Dictionary 类型,因为 XML 没有原生的键值对结构。但你可以通过一些技巧来实现从 XML 中反序列化出字典。

使用 XmlSerializer 反序列化字典的常见方法

由于 XmlSerializer 不能直接处理 Dictionary 这样的类型,通常的做法是将字典包装在一个类中,并用自定义结构模拟键值对。

步骤如下:

1. 定义一个包含字典项的类

这个类用于表示字典中的每一项(键和值):

public class KeyValueItem{    public string Key { get; set; }    public string Value { get; set; }}

2. 定义一个包装类来存放字典数据

该类包含一个 KeyValueItem 的集合,用于反序列化:

using System.Collections.Generic;using System.Xml.Serialization;[XmlRoot("Dictionary")]public class SerializableDictionary{    [XmlElement("Item")]    public List Items { get; set; } = new List();    // 提供一个属性,方便转换为 Dictionary    public Dictionary ToDictionary()    {        return Items.ToDictionary(i => i.Key, i => i.Value);    }}

3. 准备符合结构的 XML 数据

例如:

      name    张三        age    25  

4. 执行反序列化

代码示例:

using System;using System.IO;using System.Xml.Serialization;class Program{    static void Main()    {        string xml = @"      name    张三        age    25  ";        XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary));        using (StringReader reader = new StringReader(xml))        {            var result = (SerializableDictionary)serializer.Deserialize(reader);            Dictionary dict = result.ToDictionary();            foreach (var kvp in dict)            {                Console.WriteLine($"{kvp.Key}: {kvp.Value}");            }        }    }}

注意事项与扩展

这种方案适用于简单的键值对场景。如果需要支持复杂类型(如 Dictionary 或嵌套对象),可以:

修改 KeyValueItem 中的 Value 类型为 object 或泛型使用 IXmlSerializable 接口实现完全自定义序列化逻辑考虑改用 JSON 格式(如 Newtonsoft.Json 或 System.Text.Json),它们天然支持字典

替代方案:使用第三方库

如果你可以引入外部库,Newtonsoft.Json 配合 JToken 或 LINQ to XML 可以更灵活地解析 XML 并转成字典,尤其是结构不固定时。

基本上就这些。C# 原生 XML 序列化对字典支持有限,但通过封装为列表+键值结构,就能顺利反序列化出来。

以上就是C# 如何从xml中反序列化出字典(Dictionary)的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1441062.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
C# 在Unity中如何读写xml配置文件
上一篇 2025年12月17日 17:42:44
C# DataContractSerializer与XmlSerializer在处理xml上的异同
下一篇 2025年12月17日 17:42:52

相关推荐

发表回复

登录后才能评论
关注微信