使用字典将 xml 反序列化为对象 [英] Deserializing xml to object with dictionary

查看:27
本文介绍了使用字典将 xml 反序列化为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的类结构:

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):

This is XML I want to use (NOTE: I can change the xml if needed):

<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 加载到另一个字典中:

Eventually XML should be loaded into another dictionary:

private Dictionary<string, DataItem> Mydata;

推荐答案

不幸的是,通用字典不能 xml 序列化.

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

The workaround is to greate a separate field specifically to support serialization that exposes the elements of the dictionary as a list of key/value pairs. You then also have to mark the dictionary member with the XmlIgnore attribute.

或者,您可以使用 XmlSerializer 以外的其他东西(例如 DataContractSerializer) 支持字典类型.

Alternatively, you can use something other than XmlSerializer (like DataContractSerializer) which does support dictionary types.

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

Here's a link to an article which provides a good example of how modify your type to support XML Serialization with a dictionary.

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

One important point if you use this code - the serialized items may appear in arbitrary order in the output. This is a consequence of using dictinoaries (which are unordered) as the storage model for your data.

[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);   
        }   
    }   
}  

这篇关于使用字典将 xml 反序列化为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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