博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[XML]XML 文档处理 (未完待续)
阅读量:4952 次
发布时间:2019-06-11

本文共 12913 字,大约阅读时间需要 43 分钟。

Technorati 标签:
Technorati 标签:

1.winform 程序  迭代XML文档所有节点

 

Xmldocument,XmlElement,XmlText

   listbox+button

 

XML文档
Beginning Visual C#
Karli watson
7582
Professional C# 2ND Edition
Simon Robinson
7043
CS文件 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Xml;namespace XML操作{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            listBox1.Items.Clear();            XmlDocument document = new XmlDocument();            document.Load("../../XMLFile1.xml");            RecureXmlDocument(document.DocumentElement, 0);                    }        private void RecureXmlDocument(XmlNode root, int ident)        {            //如果root为空,啥也不做            if (root == null)                return;            if (root is XmlElement)            {                listBox1.Items.Add(root.Name.PadLeft(root.Name.Length+ident));                if (root.HasChildNodes)                {                    RecureXmlDocument(root.FirstChild, ident + 2);                }                if (root.NextSibling != null)                {                    RecureXmlDocument(root.NextSibling, ident);                }            }            else if (root is XmlText)            {                string text = ((XmlText)root).Value;                listBox1.Items.Add(text.PadLeft(text.Length+ident));            }        }    }}

 

 

2.修改节点的值

  InnerText,InnerXml,Value

  插入新节点

  删除节点

CS 插入,删除节点using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Xml;namespace XML操作{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            listBox1.Items.Clear();            XmlDocument document = new XmlDocument();            document.Load("../../XMLFile1.xml");            RecureXmlDocument(document.DocumentElement, 0);                    }        private void RecureXmlDocument(XmlNode root, int ident)        {            //如果root为空,啥也不做            if (root == null)                return;            if (root is XmlElement)            {                listBox1.Items.Add(root.Name.PadLeft(root.Name.Length+ident));                if (root.HasChildNodes)                {                    RecureXmlDocument(root.FirstChild, ident + 2);                }                if (root.NextSibling != null)                {                    RecureXmlDocument(root.NextSibling, ident);                }                            }            else if (root is XmlText)            {                string text = ((XmlText)root).Value;                listBox1.Items.Add(text.PadLeft(text.Length+ident));            }            else if (root is XmlComment)            {                string text = root.Value;                listBox1.Items.Add(text.PadLeft(text.Length+ident));                if(root.HasChildNodes)                    RecureXmlDocument(root.FirstChild,ident+2);                if(root.NextSibling!=null)                {                    RecureXmlDocument(root.NextSibling,ident);                }            }        }        ///         /// 添加节点        ///         ///         ///         private void button2_Click(object sender, EventArgs e)        {            XmlDocument document = new XmlDocument();            document.Load("../../XMLFile1.xml");            XmlElement root = document.DocumentElement;            XmlElement book = document.CreateElement("book");            XmlElement title = document.CreateElement("title");            XmlElement author = document.CreateElement("author");            XmlElement newcode= document.CreateElement("code");            XmlText txtTitle = document.CreateTextNode("title3");            XmlText txtAuthor = document.CreateTextNode("Author3");            XmlText code = document.CreateTextNode("1234567789");            XmlComment comment = document.CreateComment("This is common");            book.AppendChild(comment);            book.AppendChild(title);            book.AppendChild(author);            book.AppendChild(newcode);            title.AppendChild(txtTitle);            author.AppendChild(txtAuthor);            newcode.AppendChild(code);            root.InsertAfter(book, root.FirstChild);            document.Save("../../XMLFile1.xml");                    }        ///         /// 删除节点        ///         private void button3_Click(object sender ,EventArgs e)        {            XmlDocument document = new XmlDocument();            document.Load("../../XMLFile1.xml");            XmlElement root = document.DocumentElement;            if (root.HasChildNodes)            {                XmlNode book = root.LastChild;                root.RemoveChild(book);                document.Save("../../XMLFile1.xml");            }        }           }        }

 

 

 

2.Xpath

   

 

 

 

Form1.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Xml;           //XML documentnamespace Xpath{    public partial class Form1 : Form    {        private XmlDocument mDocument;        private XmlNode mCurrenNode;        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            mDocument = new XmlDocument();            mDocument.Load("../../XPathQuery.xml");            mCurrenNode = mDocument.DocumentElement;   //顶级节点            ClearListBox();        }        private void ClearListBox()        {            listBox1.Items.Clear();        }        private void button1_Click(object sender, EventArgs e)        {            Application.Exit();        }        private void displayNodeList(XmlNodeList nodelist)        {            foreach(XmlNode node in nodelist)            {                RecurseDocumentNoSiblings(node,0);            }        }        private void RecurseDocumentNoSiblings(XmlNode root,int indent)        {            if (root == null)            {                return;            }            else if (root is XmlElement)            {                listBox1.Items.Add(root.Name.PadLeft(root.Name.Length+indent));                if (root.HasChildNodes)                    RecurseDocument(root.FirstChild, indent + 2);            }            else if (root is XmlText)            {                string text = ((XmlText)root).Value;                listBox1.Items.Add(text.PadLeft(text.Length + indent));            }            else if (root is XmlComment)            {                string text = root.Value;                listBox1.Items.Add(text.PadLeft(text.Length+indent));                if (root.HasChildNodes)                {                    RecurseDocument(root, indent + 2);                }            }        }        private void RecurseDocument(XmlNode root, int indent)        {            if (root == null)            {                return;            }            else if (root is XmlElement)            {                listBox1.Items.Add(root.Name.PadLeft(root.Name.Length + indent));                if (root.HasChildNodes)                    RecurseDocument(root.FirstChild, indent + 2);                if (root.NextSibling != null)                {                    RecurseDocument(root.NextSibling, indent);                }            }            else if (root is XmlText)            {                string text = ((XmlText)root).Value;                listBox1.Items.Add(text.PadLeft(text.Length + indent));            }            else if (root is XmlComment)            {                string text = root.Value;                listBox1.Items.Add(text.PadLeft(text.Length + indent));                if (root.HasChildNodes)                {                    RecurseDocument(root, indent + 2);                }                if (root.NextSibling != null)                {                    RecurseDocument(root.NextSibling, indent);                }            }        }        private void radioButtonSelectRoot_CheckedChanged(object sender, EventArgs e)        {            mCurrenNode = mDocument.DocumentElement.SelectSingleNode("//books");            ClearListBox();            RecurseDocument(mCurrenNode, 0);        }        private void button2_Click(object sender, EventArgs e)        {            if (txtQuery.Text.Trim() == "")            {                return;            }            try            {                XmlNodeList nodelist = mCurrenNode.SelectNodes(txtQuery.Text);                ClearListBox();                displayNodeList(nodelist);            }            catch (System.Exception err)            {                MessageBox.Show(err.Message);            }        }        private void radioButtonSelectAllAhtors_CheckedChanged(object sender, EventArgs e)        {            if (mCurrenNode != null)            {                XmlNodeList nodelist = mCurrenNode.SelectNodes("//book/author");                ClearListBox();                displayNodeList(nodelist);            }            else            {                ClearListBox();            }        }        private void radioButtonselectSpecialAuthor_CheckedChanged(object sender, EventArgs e)        {            if (mCurrenNode != null)            {                XmlNodeList nodelist = mDocument.SelectNodes("//book[author='1.1']");                ClearListBox();                displayNodeList(nodelist);            }            else            {                ClearListBox();            }        }        private void radioButtonselectAllBooks_CheckedChanged(object sender, EventArgs e)        {            if (mCurrenNode != null)            {                XmlNodeList nodelist = mDocument.SelectNodes("//book");                ClearListBox();                displayNodeList(nodelist);            }            else            {                ClearListBox();            }        }        private void radioButtonSetBookAsCurrent_CheckedChanged(object sender, EventArgs e)        {            if (mCurrenNode != null)            {                mCurrenNode = mDocument.SelectSingleNode("//book[title='VS 2003']");                ClearListBox();                RecurseDocumentNoSiblings(mCurrenNode, 0);            }            else            {                ClearListBox();            }        }        private void radioButtonSetBooksAsCurrent_CheckedChanged(object sender, EventArgs e)        {            if (mCurrenNode != null)            {                mCurrenNode = mDocument.SelectSingleNode("//books");                ClearListBox();                RecurseDocumentNoSiblings(mCurrenNode, 0);            }            else            {                ClearListBox();            }        }        private void radioButtonSelectAllChildren_CheckedChanged(object sender, EventArgs e)        {            if (mCurrenNode != null)            {                XmlNodeList nodelist = mCurrenNode.SelectNodes("*");                ClearListBox();                displayNodeList(nodelist);            }            else            {                ClearListBox();            }        }    }}
XPathQuery.xml
VS 2003
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1
VS 2005
2.1
2.2
2.3
2.4
2.5
2.6
2
VS 2008
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
3
posted on
2012-02-27 15:40 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/StudyLife/archive/2012/02/27/2369946.html

你可能感兴趣的文章
接口与继承:方法覆盖(super)
查看>>
php设计模式-原型模式
查看>>
重写Repeater,使其支持空模版(列表为空时显示)
查看>>
打造自己的html5视频播放器
查看>>
spark-submit 提交任务
查看>>
POI读取Excel(xls、xlsx均可以)——(四)
查看>>
struts集合类型封装
查看>>
Cassandra issue - "The clustering keys ordering is wrong for @EmbeddedId"
查看>>
排序算法
查看>>
开发日记:在VS项目中使用SVN版本号作为编译版本号
查看>>
php静态变量与方法与phar的使用
查看>>
详细grep、sed、awk
查看>>
103. Binary Tree Zigzag Level Order Traversal
查看>>
【小结】SG生成函数(Grundy函数)
查看>>
scapy学习笔记
查看>>
hdu 5018 Revenge of GCD
查看>>
数据恢复软件使用经验-支持U盘,手机SD卡,硬盘数据,解决图片恢复后打不开的问题...
查看>>
流动python - 一个极简主义event制
查看>>
leetcode第一刷_Jump Game
查看>>
c++ 获得程序所在目录
查看>>