C#用linq to xml如何向现有文件中附加节点
刚刚接触linq to xml,想向现有的xml文件继续写入数据,而不是覆盖,应该如何解决。没有找到类似Append之类的方法。希望有人指点一下
xml
linq
c#
------解决方案-------------------- /// <summary>
/// 2、添加元素
/// </summary>
/// <param name="xmlpath">XML文件的路径</param>
private static void AddXmlElement(string xmlpath)
{
///导入XML文件
XElement xe = XElement.Load(xmlpath);
///创建一个新节点
XElement book1 = new XElement("Book",
new XAttribute("BookID", "002"),
new XElement("BookNo", "0002"),
new XElement("BookName", "Book 0002"),
new XElement("BookPrice", "50"),
new XElement("BookRemark", "This is a book 0002")
);
///添加节点到XML文件中,并保存
xe.Add(book1);
///创建一个新节点
XElement book2 = new XElement("Book",
new XAttribute("BookID", "003"),
new XElement("BookNo", "0003"),
new XElement("BookName", "Book 0003"),
new XElement("BookPrice", "30"),
new XElement("BookRemark", "This is a book 0003")