• C#温故而知新学习系列之XML编程—NET的DOM实现(四)


      前言

       我们知道我们可以利用JavaScript来操作HTML的DOM结构,以便完成一些功能,同样在.NET中我们可以利用C#来操作XML的DOM结构来完成一些功能。下面我们就来举例说明一下

      阅读目录

      一:实现步骤

      二:运行效果

      三:扩展学习

      实例

      一:实现步骤

      1:XML文件编写

      我以图书商城为例其中以我国著名的四大名著写了个XML文档图示如下

      1.1 XMLFile1.xml

         

             

      2:代码文件编写

      2.1 Form1.cs

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Text;
      using System.Windows.Forms;
      using System.Xml;

      namespace DOM
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }

              private void Form1_Load(object sender, EventArgs e)
              {

              }

              /// <summary>
              /// 使用XmlDocument对象读取xml文件
              /// </summary>
              /// <param name="sender"></param>
              /// <param name="e"></param>
              private void button1_Click(object sender, EventArgs e)
              {
                  this.listBox1.Items.Clear();
                  string strFileName = "XMLFile1.xml";
                  XmlDocument xmldoc = new XmlDocument();
                  xmldoc.Load(strFileName);
                  XmlNodeList nodelist = xmldoc.GetElementsByTagName("Name");
                  XmlNodeList nodelist2 = xmldoc.GetElementsByTagName("Author");
                  XmlNodeList nodelist3 = xmldoc.GetElementsByTagName("Money");
                  foreach (XmlNode node in nodelist)
                  {
                      this.listBox1.Items.Add(node.InnerText);
                  }
                  foreach (XmlNode node in nodelist2)
                  {
                      this.listBox2.Items.Add(node.InnerText);
                  }
                  foreach (XmlNode node in nodelist3)
                  {
                      this.listBox3.Items.Add(node.InnerText);
                  }
              }

                 
              /// <summary>
              /// 插入新的节点
              /// </summary>
              /// <param name="sender"></param>
              /// <param name="e"></param>
              private void button2_Click(object sender, EventArgs e)
              {
                  string strFileName = "XMLFile1.xml";
                  XmlDocument xmldoc = new XmlDocument();
                  xmldoc.Load(strFileName);
                  //创建<Book/>元素
                  XmlElement xe = xmldoc.CreateElement("Book");
                  xe.SetAttribute("Number", "product-005");
                  //创建<Book/>元素里面的<Name/>子元素
                  XmlElement xe_name = xmldoc.CreateElement("Name");
                  xe_name.InnerText = "《亮剑》";
                  xe.AppendChild(xe_name);
                  //创建<Book/>元素里面的<OthorName/>子元素
                  XmlElement xe_othorname = xmldoc.CreateElement("OthorName");
                  xe_othorname.InnerText = "铁血军魂";
                  xe.AppendChild(xe_othorname);
                  //创建<Book/>元素里面的<Author/>子元素
                  XmlElement xe_author = xmldoc.CreateElement("Author");
                  xe_author.InnerText = "张义";
                  xe.AppendChild(xe_author);
                  //创建<Book/>元素里面的<Money/>子元素
                  XmlElement xe_money = xmldoc.CreateElement("Money");
                  xe_money.InnerText = "150";
                  xe.AppendChild(xe_money);
                  //创建<Book/>元素里面的<Issue/>子元素
                  XmlElement xe_issue = xmldoc.CreateElement("Issue");
                  xe_issue.InnerText = "机械工业出版社";
                  xe.AppendChild(xe_issue);
                  //添加<Book/>元素
                  xmldoc.DocumentElement.AppendChild(xe);
                  //写入XML文档
                  XmlTextWriter writer = new XmlTextWriter("XMLFile1.xml", null);
                  xmldoc.WriteContentTo(writer);
                  writer.Close();
                  //再从XML文档中读取
                  this.listBox1.Items.Clear();
                  XmlNodeList nodelist = xmldoc.GetElementsByTagName("Name");
                  XmlNodeList nodelist2 = xmldoc.GetElementsByTagName("Author");
                  XmlNodeList nodelist3 = xmldoc.GetElementsByTagName("Money");
                  foreach (XmlNode node in nodelist)
                  {
                      this.listBox1.Items.Add(node.InnerText);
                  }
                  foreach (XmlNode node in nodelist2)
                  {
                      this.listBox2.Items.Add(node.InnerText);
                  }
                  foreach (XmlNode node in nodelist3)
                  {
                      this.listBox3.Items.Add(node.InnerText);
                  }
              }

          /// <summary>
              /// 查看选中书名的全部信息
              /// </summary>
              /// <param name="sender"></param>
              /// <param name="e"></param>
              private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
              {
                  string strFileName = "XMLFile1.xml";
                  XmlDocument xmldoc = new XmlDocument();
                  xmldoc.Load(strFileName);
                  string strSecarch = "BookShops/Book[Name='" + this.listBox1.SelectedItem.ToString() + "']";
                  XmlNode node = xmldoc.SelectSingleNode(strSecarch);
                  if (node != null)
                  {
                      MessageBox.Show(node.InnerText);
                  }
                  else
                  {
                      MessageBox.Show("没有找到");
                  }
              }
        }
      }  

      二:运行效果

      当我们单击“使用XmlDocument对象读取Xml”按钮后,效果如下

      

      当我们单击“插入节点”按钮后,我们发现我们的XMLFile1.xml文档的内容被更改了,同时我们再次读取XMLFile1.xml文档的时候,效果如下

       

       

       当我们选中书名后,会弹出得到该书的全部信息的提示信息

       

      三:扩展学习

      在我讲的“C#温故而知新学习系列之XML编程—Xml写入器XmlWriter类(三)”一文中,”结合使用“那一块区域也同样实现了写入XML文档元素,属性,文本的功能,对比这里的写入XML文档有什么区别呢?大家自己考虑一下

  • 相关阅读:
    C# struct 性能损失
    [leetcode] Scramble String @python
    [leetcode]Symmetric Tree @ Python
    [leetcode] Maximum Product Subarray @ python
    [leetcode]Surrounded Regions @ Python
    [leetcode]N-Queens @ Python
    [leetcode] Combinations @ Python [ask for help]
    [leetcode]Next Permutation @ Python
    [building block] merge sort @ Python
    [leetcode] Integer to Roman @ Python
  • 原文地址:https://www.cnblogs.com/menglin2010/p/2297523.html
Copyright © 2020-2023  润新知