开发者

C# hierarchy of dictionaries to xml string?

开发者 https://www.devze.com 2023-02-11 10:11 出处:网络
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)

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

精彩评论

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

关注公众号