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.xmlVS 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 阅读( ...) 评论( ...)