XML 树操作 语法

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Xml.Linq;
using System.IO;

namespace LinktoXml.Class
{
    public class XmlTreeOperator
    {
        XElement root = null;

        public XmlTreeOperator()
        {
             root = new XElement("BookList",
                                        new XElement("book",
                                            new XAttribute("name", "english"),
                                            new XAttribute("type", "education"),
                                            new XElement("author", "zhangweia"),
                                            new XElement("price", "20")),
                                        new XElement("book",
                                            new XAttribute("name", "math"),
                                            new XAttribute("type", "education"),
                                            new XElement("author", "zhangweib"),
                                            new XElement("price", "30")));
        }

        #region ---XmlLoad---
        /*
         *    public static XElement Load(String url);
         *    public static XElement Load(TextReader textReader);
         *    public static XELement load(XmlRead xmlRead);
         */
        public void XmlLoad()
        {
            root = XElement.Load("XMLFile1.xml");
        }
        #endregion

        #region ---XmlNodeAdd---
        /*               子节点的增加
         *   add(): 在当前节点子节点的末尾添加内容
         *      public void Add(Object content);
         *      public void Add(params Object[] content);
         *   AddFirst(): 用于在当前节点的第一个子节点之前添加内容
         *      public void AddFirst(Object content);
         *      public void AddFirst(params Object[] content);
         *      
         *               同级节点的增加
         *   AddAfterSelf():用于在当前节点的后面添加内容
         *      public void AddAfterSelf(Object content);
         *      public void AddAfterSelf(params Object[] content);
         *  AddBeforeSelf():用于在当前节点的前面添加内容
         *      public void AddBeforeSelf(Object content);
         *      public void AddBeforeSelf(params Object[] content);
         */

        public void XmlSubNode_Add()
        {
            XElement oneX = new XElement("book",
                                            new XAttribute("name","txl"));
            XElement twoX = new XElement("book",
                                            new XAttribute("name", "txl1"));
            root.Add(oneX,twoX);
            //root.Add(oneX);
            MessageBox.Show(root.ToString());
        }

        public void XmlSubNode_AddFirst()
        {
            XElement oneX = new XElement("book",
                                            new XAttribute("name", "txl"));
            XElement twoX = new XElement("book",
                                            new XAttribute("name", "txl1"));
            root.AddFirst(oneX, twoX);
            /*params object is sql function*/
          //  root.Elements("book").Last().AddFirst(from ele in root.Elements("book") select ele);

            MessageBox.Show(root.ToString());
        }

        public void XmlNode_AddAfterSelf()
        {
            XElement xet = root.Elements("book").First(); /*获得第一个子节点*/
            XElement oneX = new XElement("book",
                                            new XAttribute("name", "txl"));
            xet.AddAfterSelf(oneX);

            MessageBox.Show(root.ToString());
        }

        public void XmlNode_AddBeforeSelf()
        {
            XElement xet = root.Elements("book").Last(); /*获得第后一个子节点*/
            XElement oneX = new XElement("book",
                                            new XAttribute("name", "txl"));
            xet.AddBeforeSelf(oneX);

            MessageBox.Show(root.ToString());
        }
        #endregion

        #region  --XmlRemove
        /*
         *    public void RemoveAll()  //删除子节点和属性,保持标签名
         *    public void RemoveAttribute() //移除节点的所有属性
         *    
         *    public void Remove()     //移除掉整个节点
         *    
         */
        public void Xml_RemoveAll()
        {
            root.RemoveAll();
            MessageBox.Show(root.ToString());
        }

        public void Xml_Remove()
        {
            root.Elements("book").First().Remove();
            MessageBox.Show(root.ToString());
        }

        #endregion

    }
}

原文地址:https://www.cnblogs.com/zhangweia/p/1944923.html