2

Please show me how to serialize object of type IEnumerable<KeyValuePair<String, Object>>. This object do not contain custom structs, but it can (not must) contain another objects (values) of type IEnumerable<KeyValuePair<String, Object>>. As for simple it is hierarchy of dictionaries or some other generic types. I can write my own walking while serializing, but maybe you'll show me shortest way;)

Thank you!!!

Edward83
  • 6,664
  • 14
  • 74
  • 102

2 Answers2

2

you could try using XmlSerializer provided your "object" is serializable

IEnumerable<KeyValuePair<String, Object>> data = LoadData(); // however you populate your collection
var dataAsList = data.ToList(); 
XmlSerializer serializer = new XmlSerializer(dataAsList.GetType());
StringWriter sw = new StringWriter();
serializer.Serialize(sw,dataAsList );
var xml = sw.ToString();
...
...
Bala R
  • 107,317
  • 23
  • 199
  • 210
1

I would do this manually. I wouldn't trust the XML Serializer to make sense of anything like this.

Use LINQ to XML - it's the simplest.

John Saunders
  • 160,644
  • 26
  • 247
  • 397