1.winform 程序 迭代XML文档所有节点
Xmldocument,XmlElement,XmlText
listbox+button
XML文档<?xml version="1.0" encoding="utf-8" ?> <books> <book> <title>Beginning Visual C#</title> <author>Karli watson</author> <code>7582</code> </book> <book> <title>Professional C# 2ND Edition</title> <author>Simon Robinson</author> <code>7043</code> </book> </books>
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); } } } /// <summary> /// 添加节点 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> 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"); } /// <summary> /// 删除节点 /// </summary> 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 document namespace 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<?xml version="1.0" encoding="utf-8" ?> <books> <book pages="944"> <title>VS 2003</title> <author>1.1</author> <author>1.2</author> <author>1.3</author> <author>1.4</author> <author>1.5</author> <author>1.6</author> <author>1.7</author> <author>1.8</author> <author>1.9</author> <code> 1</code> </book> <book pages="1000"> <title>VS 2005</title> <author>2.1</author> <author>2.2</author> <author>2.3</author> <author>2.4</author> <author>2.5</author> <author>2.6</author> <code> 2</code> </book> <book pages="2000"> <title>VS 2008</title> <author>3.1</author> <author>3.2</author> <author>3.3</author> <author>3.4</author> <author>3.5</author> <author>3.6</author> <author>3.7</author> <author>3.8</author> <author>3.9</author> <code> 3</code> </book> </books>