开发者

C# hierarchy of dictionaries to xml string?

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 IE开发者_运维百科numerable<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!!!


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();
...
...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜