如何序列化Dictionary属性到XML里

wonderland_waterfall 2010-10-01 11:01:53
我有一个类,这个类需要序列化到一个XML文件里面。

类包含一个Dictionary<string,Dictionary<string,string>>的属性。

但是Dictionary类的属性是没有办法直接序列化成为XML的,所以用到下面的这个类来做到这一点:

[Serializable]
class ComponentsProperties : System.Xml.Serialization.IXmlSerializable
{
Dictionary<string, Dictionary<string, string>> ComponentProps { get; set; }

#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}

public void ReadXml(System.Xml.XmlReader reader)
{
// TODO implement read xml
}

public void WriteXml(System.Xml.XmlWriter writer)
{
// TODO implement write xml
}
#endregion
}

现在把要序列化的class里面的Dictionary<...>属性换成上面的ComponentsProperties属性,从而让它可以被序列化:

public Dictionary<string, Dictionary<string,string>> Settings { get; set; }

替换成了

public ComponentsProperties ComponentSettings { get; set; }

但是访问属性的时候,原来的Settings[key]=value应该变成怎样?

最重要的是,上面的那个类需要实现的那几个method都是做什么的,应该怎样实现啊?

非常感谢。

P.S. 序列化以后的文件可以包含这个:

Dictionary<string, Dictionary<string,string>>
ComponentOne,
SettingOne, SettingOneValue
SettingTwo,SettingTwoValue
ComponentTwo,
AnotherSettingOne, AnotherSettingOneValue,
AnotherSettingTwo, AnotherSettingTwoValue
--------------------------------------
<ComponentSettings>
<ComponentOne>
<SettingOne>SettingOneValue</SettingOne>
<SettingTwo>SettingTwoValue</SettingTwo>
</ComponentOne>
<ComponentTwo>
<AnotherSettingOne>AnotherSettingOneValue</AnotherSettingOne>
<AnotherSettingTwo>AnotherSettingTwoValue</AnotherSettingTwo>
</ComponentTwo>
</ComponentSettings>
...全文
578 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
vip__888 2010-10-02
  • 打赏
  • 举报
回复
遍历不行 么?
alan001 2010-10-02
  • 打赏
  • 举报
回复
up................
jshi123 2010-10-02
  • 打赏
  • 举报
回复
可以从Dictionary泛型类派生并实现IXmlSerializable

public class ComponentsProperties : Dictionary<string, Dictionary<string,string>>, System.Xml.Serialization.IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}

public void ReadXml(System.Xml.XmlReader reader)
{
if (reader.IsEmptyElement)
return;

reader.Read();
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
string key = reader.Name;
this[key] = new Dictionary<string, string>();
if (!reader.IsEmptyElement)
{
reader.ReadStartElement();
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
this[key][reader.Name] = reader.ReadElementString();
}
reader.Read();
}
}

public void WriteXml(System.Xml.XmlWriter writer)
{
foreach (string key in this.Keys)
{
writer.WriteStartElement(key);
foreach (string key1 in this[key].Keys)
writer.WriteElementString(key1, this[key][key1]);
writer.WriteEndElement();
}
}
#endregion
}


调用时候的写法和原来是一样的,例如:

public class TestClass
{
[XmlElement("ComponentSettins")]
public ComponentsProperties Settings { get; set; }
}

void DoTest()
{
TestClass test = new TestClass();
test.Settings = new ComponentsProperties();
test.Settings["ComponentOne"] = new Dictionary<string, string>();
test.Settings["ComponentOne"]["SettingOne"] = "SettingOneValue";
test.Settings["ComponentOne"]["SettingTwo"] = "SettingTwoValue";
test.Settings["ComponentTwo"] = new Dictionary<string, string>();
test.Settings["ComponentTwo"]["AnotherSettingOne"] = "AnotherSettingOneValue";
test.Settings["ComponentTwo"]["AnotherSettingTwo"] = "AnotherSettingTwoValue";

XmlSerializer xs = new XmlSerializer(typeof(TestClass));
FileStream fs = new FileStream(@"d:\test.xml", FileMode.Create);
xs.Serialize(fs, test);
fs.Close();
}

wuyq11 2010-10-01
  • 打赏
  • 举报
回复
Dictionary本身支持Serialization。key,value都是Serializable
不能直接xml序列化, 转化成list再序列化

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧