遍历一个类的字段和值

mozc是要遍历的类名

p.name是字段名

p.GetValue是字段名对应的值

foreach (System.Reflection.PropertyInfo p in mozc.GetType().GetProperties())
{
XmlElement name = doc.CreateElement(p.Name);
name.InnerText = p.GetValue(mozc, null).ToString();

work1.AppendChild(name);
}

原文地址:https://www.cnblogs.com/zchbiji/p/9039861.html