使用字典将 xml 反序列化为对象

2023-12-01

这是我的班级结构:

class DataItem
{
   public string Id { get; set; }
   public string Type { get; set; }

   private Dictionary<string, DataProperty> properties = new Dictionary<string, DataProperty>();

   public Dictionary<string, DataProperty> Properties
   {
       get { return properties; }
       set { properties = value; }
   }
}

public class DataProperty
{    
      public string Key { get; set; }
      public string Value { get; set; }
      public bool Required { get; set; }
}

这是我想使用的 XML (注意:如果需要,我可以更改 xml):

<Data>
  <DataItem>
    <id>ph15</id>
    <type>core</type>
    <properties>
      <DataProperty>
        <key>size</key>
        <value>50%</value>
        <required>false</required>
      </DataProperty>
      <DataProperty>
        <key>color</key>
        <value>red</value>
        <required>true</required>
      </DataProperty>
    </properties>
  </DataItem>
  <DataItem>
    <id>ph234</id>
    <type>core</type>
    <properties>
    </properties>
  </DataItem>
</Data>

最终 XML 应该被加载到另一个字典中:

private Dictionary<string, DataItem> Mydata;

不幸的是,通用字典不可进行 xml 序列化。

解决方法是专门创建一个单独的字段来支持序列化,将字典的元素公开为键/值对列表。然后,您还必须用以下标记来标记字典成员XmlIgnore属性。

或者,您可以使用 XmlSerializer 以外的其他东西(例如数据契约序列化器)它确实支持字典类型。

这是一个链接到一篇文章它提供了一个很好的示例,说明如何修改类型以支持使用字典的 XML 序列化。

如果您使用此代码,有一点很重要 - 序列化的项目可能以任意顺序出现在输出中。这是使用字典(无序)作为数据存储模型的结果。

[XmlIgnore()]   
public Dictionary<string, DataProperty> Properties
{   
    set { properties = value; }   
    get { return properties ; }   
}


[XmlArray("Stuff")]   
[XmlArrayItem("StuffLine", Type=typeof(DictionaryEntry))]   
public DictionaryEntry[] PropertiesList
{   
    get  
    {   
        //Make an array of DictionaryEntries to return   
        DictionaryEntry[] ret=new DictionaryEntry[Properties.Count];   
        int i=0;   
        DictionaryEntry de;   
        //Iterate through Stuff to load items into the array.   
        foreach (KeyValuePair<string, DataProperty> props in Properties)   
        {   
            de = new DictionaryEntry();   
            de.Key = props.Key;   
            de.Value = props.Value;   
            ret[i]=de;   
            i++;   
        }   
        return ret;   
    }   
    set  
    {   
        Properties.Clear();   
        for (int i=0; i<value.Length; i++)   
        {   
            Properties.Add((string)value[i].Key, (DataProperty)value[i].Value);   
        }   
    }   
}  
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用字典将 xml 反序列化为对象 的相关文章

随机推荐

  • 如何使用 Google Drive API 检索最近的已删除文件列表

    我最近删除了 Google Drive 中的大量文件 并将它们移至垃圾箱 我想永久删除它们 但垃圾箱中的文件不能按删除日期排序 只能按修改日期排序 删除时不会更新 因此 我想利用 Google Drive API 来枚举垃圾并确定其被删除的
  • 相当于 with(来自 Pascal)到 C/C++

    相当于什么with从Pascal语言到C C 语言 A with语句是引用记录字段或对象的字段 属性和方法的简写 Example With Object do begin Width 200 Height 300 end 相当于 Objec
  • 为什么在Java的Object类中声明wait()和notify()?

    为什么是wait and notify 中声明的方法Object类 而不是Thread class 因为 您等待给定的对象 或者具体来说 它的监视器 来使用此功能 我认为您可能误解了这些方法的工作原理 它们不只是处于线程粒度级别 即not只
  • IndentationError:需要一个缩进块[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 目前不接受答案 有人可以告诉我在这个 Python 代码示例中我做错了什么吗 for i in range len Adapters print Adapters i 我正在尝试列出 打印数
  • Webdriver selenium,java.lang.NullPointerException,尝试查找 Webelement 时

    编写了完整的代码来从 Excel 获取数据并登录 Gmail 但在尝试这样做时 我的浏览器已打开 所需的页面也已打开 并且登录 ID 已从 Excel 中选取并存储在变量 sUsername 中 但无法将 xpath 定位为 element
  • R geom_point 和 ggmap

    我想在 R 中绘制一个 ggmap 例如澳大利亚的 ggmap 并有一层数据点 其标记对应于以下数据指定的大小 sitename lat lon sitenum Sydney 34 151 1 Melbourne 37 144 4 Adel
  • Mongodb 节省了一天时间 - 时区问题

    我以 MM DD YYYY 的角度格式发布日期 但是当它保存在 Mongodb 中时 它会少一天保存 ISO 格式 我正在使用 MEAN 堆栈进行开发 例如 来自角度 10 03 2016 mongodb ISODate 2016 10 0
  • 从 python 子进程运行 linux grep 命令

    我知道已经有关于如何在 python 中使用子进程来运行 linux 命令的帖子 但我只是无法获得正确的语法 请帮忙 这是我需要运行的命令 sbin ifconfig eth1 grep inet addr awk F print 2 aw
  • mysql 检查数字是否在逗号分隔列表中

    我有一个这样的表 UID int NUMBERS blob 1 1 13 15 20 2 3 10 15 20 3 3 15 我想测试 3 和 15 是否在名为 NUMBERS 的 blob 中 并且可以看到LIKE 不能使用 仅选择 ID
  • 合并2个没有重复键的数组

    我有两个如下所示的数组 想要将它们合并在一起 而不是将重复的键放入新数组中 array1 Array 0 gt Array a gt Array p gt Array a gt 1 a s gt Array p gt Array o gt
  • Type.GetType("namespace.a.b.ClassName") 返回 null

    这段代码 Type GetType namespace a b ClassName returns null 我的用途是 using namespace a b 该类型存在 它位于不同的类库中 我需要通过以字符串形式给出的名称来获取它 Ty
  • 如何快速检测哪个图像被点击

    我在 ViewController 上创建了 6 个 UIImageView 稍后我将向所有这些视图添加 TapGestureRecognizer 我想让它根据单击的图像 另一个 ViewController 将打开并显示某些信息 为此 我
  • Sql Server Ce 3.5 身份插入

    Sql Server CE 中的标识列出现问题 使用服务器资源管理器时 在VS2008中 执行以下脚本 SET IDENTITY INSERT testTable ON 插入 testTable id name 值 1 Something
  • 如何限制创建的Thread对象的数量?

    我想在下面的代码中限制数量class f同时运行的对象 即限制同时工作的线程数量 我怎样才能做到这一点 usr bin env python import random import time from threading import T
  • 线程“main”java.sql.SQLException中的异常:靠近“s”:语法错误

    我编写了一个程序 用单词列表填充数据库 问题是 每次我尝试运行代码时 它都会抛出 线程 main java sql SQLException中的异常 接近 s 语法错误 我意识到这个论坛上也有人提出了类似的问题 但在我的代码上下文中我无法纠
  • CardView突然变黑了

    我有一个问题CardView in the RecyclerView My CardView突然变黑了 我的RatingBar变成蓝色 我正在使用InfiniteRecyclerView但将其改为简单的RecyclerView没有影响 我可
  • 在Python中重新打开文件?

    假设我有这个简单的 python 脚本 file open C some text txt print file readlines print file readlines 运行时 第一个打印会打印包含文件文本的列表 而第二个打印会打印空
  • pheatmap 中缺少注释和颜色

    使用 pheatmap R 包 我需要制作带有自定义颜色集注释的热图 我使用以下代码 Make a matrix mat lt replicate 23 rnorm 23 colnames mat lt c 1 2 3plus3reseq
  • Material-UI Button 组件如何推断传递给“component” prop 的组件的 props?

    谁能解释一下 Material UI 如何扩展其 propsButton如果我在component prop interface MyLinkProps extends ButtonBaseProps someRandomProp stri
  • 使用字典将 xml 反序列化为对象

    这是我的班级结构 class DataItem public string Id get set public string Type get set private Dictionary