C#基础:XML文档对象模型DOM

XML文档对象模型(Document Object Model,DOM)是一组已非常直观的方式访问和处理XML的类。位于System.Xml中

        XmlNode:XML文档中的一个节点               

             获取节点值:

                InnerText:获取节点中的文本

                InnerXml:返回字节的及内容

                Value:XmlText,XmlComment,XmlAttribute会返回Value值

             创建节点:
                CreateNode:创建任意类型节点

                CreateElement:创建XmlElement类型的节点

                CreateAttribute:创建XMLAttribute类型

                CreateTextNode:创建XMLTextNode类型节点

                CreateComment:创建注释

            插入节点:

                 AppendChild:添加子节点

                InsertAfter:在参考节点之后插入新节点

                InserBefore:在参考节点之前插入新节点

  1.             //Xml对象
  2.             XmlDocument document = new XmlDocument();
  3.             document.Load(@"path\Books.xml");
  4.             // 根节点
  5.             XmlElement root = document.DocumentElement;
  6.             //XmlElement类型 子节点
  7.             XmlElement newBook = document.CreateElement("book");
  8.             XmlElement newTitle = document.CreateElement("title");
  9.             XmlElement newAuthor = document.CreateElement("author");
  10.             XmlElement newCode = document.CreateElement("code");
  11.             //XmlText类型 子节点
  12.             XmlText title = document.CreateTextNode("Beginning Visual C#");
  13.             XmlText author = document.CreateTextNode("Ben");
  14.             XmlText code = document.CreateTextNode("314418");
  15.            //注释
  16.             XmlComment comment = document.CreateComment("The previous edition");
  17.  
  18.             // 添加子节点
  19.             newBook.AppendChild(comment);
  20.             newBook.AppendChild(newTitle);
  21.             newBook.AppendChild(newAuthor);
  22.             newBook.AppendChild(newCode);
  23.             //添加<></>标记之间的text值
  24.             newTitle.AppendChild(title);
  25.             newAuthor.AppendChild(author);
  26.             newCode.AppendChild(code);
  27.             //添加位置,最后一个
  28.             root.InsertAfter(newBook, root.LastChild);
  29.             //保存
  30.             document.Save(@"path\Books.xml");

              选择节点:

                      SelectSingleNode:选择一个节点

                      SelectNodes:以XmlNodesList类的形式返回一个节点集合

              删除节点:

                     RemoveAll:删除所有子节点

                     RemoveChild:删除一个子节点

  1.                     XmlNode book=root.LastChild;
  2.                     root.RemoveChild(book);

        XmlDocument:XML文档对象

  1.             XmlDocument document=new XmlDocument();
  2.             document.Load("path\xxx.xml");

        XmlElement:XML文档的一个元素

  1.             XmlElement element=document.DocumentElement;

               FirstChild:返回第一个子节点

               LastChild:返回最后一个字节点

               ParentNode:返回父节点

               NextSibling:返回下个节点

              HasChildNodes:检查当前元素是否有子元素

        XmlAttribute:XML元素的属性 sex="female"

              <author sex='female'>"Ben"</author>

        XmlText:<></>标记之间的文本 Beginning Visual C#

               <title>Beginning Visual C#</title>

        XmlComment:XML文档的注释

               <!--这个是注释-->

        XmlNodeList:XML文档节点集合

猜你喜欢

转载自blog.csdn.net/QQhelphelp/article/details/86529498