XML Serializing a class with a Dictionary<string, List<string>> object

Posted by Matt on Stack Overflow See other posts from Stack Overflow or by Matt
Published on 2010-06-11T23:39:55Z Indexed on 2010/06/11 23:42 UTC
Read the original article Hit count: 258

Filed under:
|
|

Is it possible to implement IXmlSerializable and in my XML file capture an object of type Dictionary> ?

I have the following

public class coolio : IXmlSerializable
{
private int a;
private bool b;
private string c;
private Dictionary<string, List<string>> coco;

public coolio(int _a, bool _b, string _c, Dictionary<string, List<string>> _coco)
{
a=_a;
b=_b;
c=_c;
coco=_coco;
}

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void WriteXml(XmlWriter writer)
{
const string myType = "coolio";
writer.WriteStartElement(myType);
writer.WriteAttributeString("a", a.ToString());
writer.WriteAttributeString("b", b.ToString());
writer.WriteAttributeString("c", c);

// How do I add a subelement for Dictionary<string, List<string>> coco?

writer.WriteEndElement();
}

public void ReadXml(XmlReader reader)
{
if (reader.MoveToContent() != XmlNodeType.Element || reader.LocalName != "coolio") return;
a= int.Parse(reader["a"]);
b = bool.Parse(reader["b"]);
c= reader["c"];

// How do I read subelement into Dictionary<string, List<string>> coco?
}
}

But I am stumped as to how I could add the Dictionary> (XML seriliazed to my XML file)

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics