C# 使用XElement 如何在已有的指定目录XML下追加内容
给个范例最好了,谢谢
XmlWriter好像不能追加在以后的文件后,将会把已有内容清空,无语了

解决方案 »

  1.   

    public static void Insert(string path, string node, string element, string attribute, string value)
      {
      try
      {
      XmlDocument doc = new XmlDocument();
      doc.Load(path);
      XmlNode xn = doc.SelectSingleNode(node);
      if (element.Equals(""))
      {
      if (!attribute.Equals(""))
      {
      XmlElement xe = (XmlElement)xn;
      xe.SetAttribute(attribute, value);
      }
      }
      else
      {
      XmlElement xe = doc.CreateElement(element);
      if (attribute.Equals(""))
      {
      xe.InnerText = value;
      }
      else
      {
      xe.SetAttribute(attribute, value);
      }
      xn.AppendChild(xe);
      }
      doc.Save(path);
      }
      catch { }
      }
    linq
    XElement mXml = XElement.Load("Test.xml");  //加载需要操作的XML文件
    mXml .Add(new XElement("newNode", "newNodes"));
      

  2.   

    给 节点赋值或添加节点,然后 Save 保存就行了。