110,534
社区成员
发帖
与我相关
我的任务
分享
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();
}