写XML

//创XML建对象
XmlDocument doc = new XmlDocument();
//bool a = false;
//声明根节点
XmlElement books;
//判断文件是否存在
if (File.Exists(path))
{
//该文件存在
//加载文件
doc.Load(path);
//获得根节点
books = doc.DocumentElement;
//a = true;
}
else//该文件不存在
{
//创建声明
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
//创建根节点
books = doc.CreateElement("works");
doc.AppendChild(books);

}
//开始正常写入信息就可以了
XmlElement book1 = doc.CreateElement("work");
book1.SetAttribute("state", "1");
books.AppendChild(book1);
foreach (System.Reflection.PropertyInfo p in work.GetType().GetProperties())
{
XmlElement name = doc.CreateElement(p.Name);
name.InnerText = p.GetValue(work, null).ToString();
book1.AppendChild(name);
}

//遍历参考随笔遍历类

doc.Save(path);
return info;

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