将任意对象转储到XML(绕过默认序列化)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将任意对象转储到XML(绕过默认序列化)相关的知识,希望对你有一定的参考价值。

Useful in cases where the object author has specified their own serialization that doesn't work for you.
  1. public static string ObjectToString(object o)
  2. {
  3. XDocument doc = new XDocument();
  4. string objectType = o.GetType().ToString();
  5. doc.AddFirst(new XElement(objectType));
  6.  
  7. foreach (PropertyInfo pi in o.GetType().GetProperties())
  8. {
  9. doc.Root.Add(new XElement(pi.Name, Convert.ToString(pi.GetValue(o, null))));
  10. }
  11. return doc.ToString();
  12. }

以上是关于将任意对象转储到XML(绕过默认序列化)的主要内容,如果未能解决你的问题,请参考以下文章