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文档有什么区别呢?大家自己考虑一下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值