XMl的实际运用

图片效果

1  创建Xm
    private void CreateXML_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            //创建根节点
            XmlElement root = doc.CreateElement("bookstore");
            doc.AppendChild(root);
            #region   记录一本书
            // 创建第二级节点
            XmlElement node = doc.CreateElement("book"); //其实也可以用XmlNode接收,但是他没有设置属性的方法
            node.SetAttribute("Type", "必修课");
            node.SetAttribute("ISBN", "7-111-19149-2");
            root.AppendChild(node);


            XmlElement titleNode = doc.CreateElement("title");
            titleNode.InnerText = "数据结构";
            node.AppendChild(titleNode);

            XmlElement authorNode = doc.CreateElement("author");
            XmlText xmlTextauthor = doc.CreateTextNode("严蔚敏");
            authorNode.AppendChild(xmlTextauthor);
            node.AppendChild(authorNode);

            XmlElement priceNode = doc.CreateElement("price");
            XmlText xmlTextprice = doc.CreateTextNode("30.00");
            priceNode.AppendChild(xmlTextprice);
            node.AppendChild(priceNode);
            #endregion

            #region   记录二本书
            // 创建第二级节点
            XmlElement nodeSencode = doc.CreateElement("book"); //其实也可以用XmlNode接收,但是他没有设置属性的方法
            nodeSencode.SetAttribute("Type", "必修课");
            nodeSencode.SetAttribute("ISBN", "7-111-19149-3");
            root.AppendChild(nodeSencode);


            XmlElement titleNode2 = doc.CreateElement("title");
            titleNode2.InnerText = "路由型与交换型互联网基础";
            nodeSencode.AppendChild(titleNode2);


            XmlElement authorNode2 = doc.CreateElement("author");
            XmlText xmlTextauthor2 = doc.CreateTextNode("程庆梅");
            authorNode2.AppendChild(xmlTextauthor2);
            nodeSencode.AppendChild(authorNode2);



            XmlElement priceNode2 = doc.CreateElement("price");
            XmlText xmlTextprice2 = doc.CreateTextNode("27.00");
            priceNode2.AppendChild(xmlTextprice2);
            nodeSencode.AppendChild(priceNode2);
            #endregion
            try
            {
                doc.Save("d:\cd.xml");
            }
            catch (Exception ex)
            {

                MessageBox.Show("发生异常" + ex.Message);
                return;
            }

            MessageBox.Show("xml创建成功");

        }

2 获得所有数据

    private void getAllData_Click(object sender, EventArgs e)
        {
          
            XmlDocument doc = new XmlDocument();
            doc.Load("d:\cd.xml");
            XmlNode parent = doc.DocumentElement;


            XmlNodeList nodeList = parent.ChildNodes;

            List<BookModel> bookModeList = new List<BookModel>();
            foreach (XmlNode item in nodeList)
            {
                BookModel bookModel = new BookModel();
                // 将节点转换为元素,便于得到节点的属性值
                XmlElement xe = (XmlElement)item;
                // 得到Type和ISBN两个属性的属性值
                bookModel.BookISBN = xe.GetAttribute("ISBN").ToString();
                bookModel.BookType = xe.GetAttribute("Type").ToString();
                // 得到Book节点的所有子节点
                XmlNodeList xnl0 = xe.ChildNodes;
                bookModel.BookName = xnl0.Item(0).InnerText;
                bookModel.BookAuthor = xnl0.Item(1).InnerText;
                bookModel.BookPrice = Convert.ToDouble(xnl0.Item(2).InnerText);
                bookModeList.Add(bookModel);
            }
           
            //XmlDocument xmlDoc = new XmlDocument();
            //XmlReaderSettings settings = new XmlReaderSettings();
            //settings.IgnoreComments = true;//忽略文档里面的注释
            //XmlReader reader = XmlReader.Create(@"....Book.xml", settings);
            //xmlDoc.Load(reader);
            //reader.Close();

            dgvBookInfo.DataSource = bookModeList;
        }

3 增加一个节点

    private void AddBook_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("d:\cd.xml");
            XmlNode parent = doc.DocumentElement;


          XmlElement xleKey=doc.CreateElement("book");


          XmlAttribute xleType= doc.CreateAttribute("Type");
          xleType.InnerText = "必修";
          xleKey.SetAttributeNode(xleType);
          XmlAttribute ISBN = doc.CreateAttribute("ISBN");
          ISBN.InnerText = "7-11-1893-4";
          xleKey.SetAttributeNode(ISBN);



          XmlElement xleTitle = doc.CreateElement("title");
          xleTitle.InnerText = "资本主义论";
          xleKey.AppendChild(xleTitle);


          XmlElement xleaAuthor = doc.CreateElement("author");
          xleaAuthor.InnerText = "茅盾";
          xleKey.AppendChild(xleaAuthor);

          XmlElement xleaPrice = doc.CreateElement("price");
          xleaPrice.InnerText = "25.00";
          xleKey.AppendChild(xleaPrice);


          parent.AppendChild(xleKey);
          doc.Save("d:\cd.xml");


        }

4  删除一个节点
       private void DelData_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("d:\cd.xml");
            XmlNode parent = doc.DocumentElement;
           // string isbn=dgvBookInfo.CurrentRow.Cells[1].Value.ToString();
           // string spath = "/bookstore/book[ISBN="" + isbn + ""]";

        
            string strPath = string.Format("/bookstore/book[@ISBN="{0}"]", dgvBookInfo.CurrentRow.Cells[1].Value.ToString());
            XmlElement selectXe = (XmlElement)parent.SelectSingleNode(strPath);
            selectXe.ParentNode.RemoveChild(selectXe);
            doc.Save("d:\cd.xml");


        }

5 修改一个节点
  XmlDocument doc = new XmlDocument();
            doc.Load("d:\cd.xml");
            XmlNode parent = doc.DocumentElement;


             string strPath = string.Format("/bookstore/book[@ISBN="{0}"]", dgvBookInfo.CurrentRow.Cells[1].Value.ToString());
             XmlElement selectXe = (XmlElement)parent.SelectSingleNode(strPath);  //selectSingleNode 根据XPath表达式,获得符合条件的第一个节点.

             //selectXe.SetAttribute("Type", dgvBookInfo.CurrentRow.Cells[0].Value.ToString());

             selectXe.GetElementsByTagName("title").Item(0).InnerText = "我被改了";

             doc.Save("d:\cd.xml");


存储格式
1: <?xml version="1.0" encoding="utf-8"?>
   2: <bookstore>
   3:   <!--记录书本的信息-->
   4:   <book Type="必修课" ISBN="7-111-19149-2">
   5:     <title>数据结构</title>
   6:     <author>严蔚敏</author>
   7:     <price>30.00</price>
   8:   </book>
   9:   <book Type="必修课" ISBN="7-111-19149-3">
  10:     <title>路由型与交换型互联网基础</title>
  11:     <author>程庆梅</author>
  12:     <price>27.00</price>
  13:   </book>
  14:   <book Type="必修课" ISBN="7-111-19149-4">
  15:     <title>计算机硬件技术基础</title>
  16:     <author>李继灿</author>
  17:     <price>25.00</price>
  18:   </book>
  19:   <book Type="必修课" ISBN="7-111-19149-5">
  20:     <title>软件质量保证与管理</title>
  21:     <author>朱少民</author>
  22:     <price>39.00</price>
  23:   </book>
  24:   <book Type="必修课" ISBN="7-111-19149-6">
  25:     <title>算法设计与分析</title>
  26:     <author>王红梅</author>
  27:     <price>23.00</price>
  28:   </book>
  29:   <book Type="选修课" ISBN="7-111-19149-1">
  30:     <title>计算机操作系统</title>
  31:     <author>7-111-19149-1</author>
  32:     <price>28</price>
  33:   </book>
  34: </bookstore>
原文地址:https://www.cnblogs.com/cdaq/p/4941833.html