Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > .NET平台开发必须掌握的XML知识(二)
【标  题】:.NET平台开发必须掌握的XML知识(二)
【关键字】:.NET,XML
【来  源】:http://blog.csdn.net/Terry001/archive/2007/02/22/1512459.aspx

.NET平台开发必须掌握的XML知识(二)

Your Ad Here


/*********    程序设计目的:往已经存在的XML文件中添加新的节点,并把节点读出来显示在列表框中,同时练习怎么删除节点。   ********/
/*********    一共用了一个窗体,四个按纽,一个列表框                                                                  ********/
/*********    主要运用的System.Xml命名空间的类的属性和方法                                                            ********/
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 操作XML文件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoopXml_Click(object sender, EventArgs e)
        {
            //Clear ListBox
            listBox1XmlNodes.Items.Clear();

            //Load the XML document
            XmlDocument document = new XmlDocument();
            document.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");

            //Loop XML document
            recurseXmlDocument((XmlNode)document.DocumentElement, 0);

        }

        //读出节点并显示出来
        private void recurseXmlDocument(XmlNode root, int indent)
        {
            //Make sure we don't do anything if the root is null
            if (root == null)
            {
                return;
            }
            if (root is XmlElement) //Root is an XmlElement type
            {
                //first print the name
                listBox1XmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));

                //Then check if there are any child nodes and if there are,call this
                //method anain to print them
                if (root.HasChildNodes)
                {
                    recurseXmlDocument(root.FirstChild, indent + 2);
                }

                //Finally check tosee if there are any siblings and if there are
                //call this method again to have them printed
                if (root.NextSibling != null)
                    recurseXmlDocument(root.NextSibling, indent);
            }
            else if(root is XmlText)
            {
                //Print the text
                string text  = ((XmlText)root).Value;
                listBox1XmlNodes.Items.Add(text.PadLeft(text.Length+indent));
            }
            else if(root is XmlComment)
            {
                //print text
                string text = root.Value;
                listBox1XmlNodes.Items.Add(text.PadLeft(text.Length+indent));

                //Then check if there are any child nodes,and jf there are
                //method again to print them
                if(root.HasChildNodes)
                {
                    recurseXmlDocument(root.FirstChild,indent+2);
                }
                  
               
                 //Finally,check to see if there are any siblings,and if there are
                 //call this method again to have them printed.
                if (root.NextSibling!=null)
                {
                    recurseXmlDocument(root.NextSibling,indent);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
             MessageBox.Show("关闭窗口!");
             this.Close();
        }

        //添加节点
        private void btnCreateNode_Click(object sender, EventArgs e)
        {
            //Load the XML document
            XmlDocument document = new XmlDocument();
            document.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");

            //Get the root element
            XmlElement root = document.DocumentElement;

            //create the new nodes
            XmlElement newStory = document.CreateElement("story");
            XmlElement newTitle = document.CreateElement("title");
            XmlElement newAuthor = document.CreateElement("author");
            XmlElement newName = document.CreateElement("name");
            XmlElement newNationality = document.CreateElement("nationality");
            XmlElement newRating = document.CreateElement("rating");

            XmlComment comment = document.CreateComment("This story is the story you are reading!");
            XmlText title = document.CreateTextNode("美丽人生");
            XmlText name = document.CreateTextNode("王亚楠");
            XmlText nationality = document.CreateTextNode("中国");
            XmlText rating = document.CreateTextNode("智慧,美丽,时尚,活力");
          

            //Insert the element
            newStory.AppendChild(comment);
            newStory.AppendChild(newTitle);
            newStory.AppendChild(newAuthor);
            newStory.AppendChild(newRating);

            newTitle.AppendChild(title);
          

            newAuthor.AppendChild(newName);
            newAuthor.AppendChild(newNationality);
          

            newName.AppendChild(name);
            newNationality.AppendChild(nationality);

            newRating.AppendChild(rating);

          
            root.InsertAfter(newStory,root.LastChild);
            root.InsertAfter(newStory,root.FirstChild);
            document.Save(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");

         
        }

        //删除节点
        private void btnDeleteNode_Click_1(object sender, EventArgs e)
        {
            //Load XML document
            XmlDocument document = new XmlDocument();
            document.Load(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");

            //Get the root element
            XmlElement root = document.DocumentElement;
            // Find the node.root is the <story>tag,soits last child
            //while will be the last <book> node.
            if (root.HasChildNodes)
            {
                XmlNode story = root.LastChild;

                //Delete the child
                root.RemoveChild(story);

                //Save the document story to disk
                document.Save(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\XMLFile1.xml");
            }
        }


 
    }
}


//本程序是先利用btnCreateNode按纽往XML文件中写入数据,然后再利用btnLoopXml按纽读出数据显示在列表框listBox1XmlNodes中
//读数据时主要利用了recurseXmlDocument(XmlNode root, int indent)方法迭代XML中的元素
//问题的关键在于recurseXmlDocument(XmlNode root, int indent)方法的编写
 

Object类的用法(一):【上一篇】
Windows Forms 2.0 Programming 花边随笔(001):【下一篇】
【相关文章】
  • .net2.0 中的持续集成
  • asp.net 2.0 Profile 的一些注意事项(序列化)
  • 毕业了,发布一下我的求职简历(.net程序员)
  • .NET2005下创建单元测试的方法
  • Msdn 杂志 asp.net ajax 文章汇集
  • .NET2005下单元测试中Assert类的用法。
  • .NET平台开发必须掌握的XML知识(一)
  • 2006 年 XML 大事记
  • 如何让.NET中的强类型的排序列表SortedList支持重复键
  • ASP.NET AJAX 动手教程系列,硬盘输出缓存和RSS工具包CodePlex项目,以及采访我的podcast
  • 【随机文章】
  • LDAP启动故障恢复
  • 流光教程一
  • CAM350 操作要点及注意事项
  • TCard V1.1 (无需DLL扑克图形控件)
  • “未能创建 Mutex”问题的解决
  • C++资源之不完全导引 [下]
  • 微软认证数据库管理员 (MCDBA)
  • Webwork 2.2的Action是否使用Spring的prototype?获取的性能对比
  • 在线刷QQ空间人气 V2.1测试版
  • 经济普查全国数据库优化方案
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.