使用XmlDocument创建XML文档及增加删除更新节点
2008-07-08 07:08
691 查看
using System; using System.Windows.Forms; using System.Xml; namespace XMLDemo { public partial class FrmDOM : Form { public FrmDOM() { InitializeComponent(); } private void btnLoad_Click(object sender, EventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Books.xml"); MessageBox.Show(xmlDoc.InnerXml); } private void btnCreate_Click(object sender, EventArgs e) { //xml文档 XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(dec); //创建根节点 XmlElement root = xmlDoc.CreateElement("Books"); xmlDoc.AppendChild(root); //节点及元素 XmlNode book = xmlDoc.CreateElement("Book"); XmlElement title = GetXmlElement(xmlDoc, "Title", "Window Form"); XmlElement isbn = GetXmlElement(xmlDoc, "ISBN", "111111"); XmlElement author = GetXmlElement(xmlDoc, "Author", "amandag"); XmlElement price = GetXmlElement(xmlDoc, "Price", "128.00"); price.SetAttribute("Unit", "¥"); book.AppendChild(title); book.AppendChild(isbn); book.AppendChild(author); book.AppendChild(price); root.AppendChild(book); xmlDoc.Save("Books.xml"); MessageBox.Show("数据已写入!"); } private void btnInsert_Click(object sender, EventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Books.xml"); XmlNode root = xmlDoc.SelectSingleNode("Books"); XmlElement book = xmlDoc.CreateElement("Book"); XmlElement title = GetXmlElement(xmlDoc, "Title", "ASP.NET"); XmlElement isbn = GetXmlElement(xmlDoc, "ISBN", "222222"); XmlElement author = GetXmlElement(xmlDoc, "Author", "moon"); XmlElement price = GetXmlElement(xmlDoc, "Price", "111.00"); price.SetAttribute("Unit", "{1}quot;); book.AppendChild(title); book.AppendChild(isbn); book.AppendChild(author); book.AppendChild(price); root.AppendChild(book); xmlDoc.Save("Books.xml"); MessageBox.Show("数据已插入!"); } private void btnUpdate_Click(object sender, EventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Books.xml"); //方法1:获取Books//Book节点的第一个子节点 XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes; XmlElement xe = null; //遍历所有子节点 foreach (XmlNode xn in nodeList) { //将子节点类型转换为XmlElement类型 xe = (XmlElement)xn; if (xe.Name == "Author" && xe.InnerText == "amandag") { xe.InnerText = "高歌"; } if (xe.GetAttribute("Unit") == "¥") { xe.SetAttribute("Unit", "{1}quot;); } } //方法2: XmlNode node = xmlDoc.SelectSingleNode("Books//Book[Author=\"moon\"]//Author"); if(node != null) { node.InnerText = "宝贝"; } xmlDoc.Save("Books.xml"); MessageBox.Show("数据已更新!"); } private void btnDelete_Click(object sender, EventArgs e) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("Books.xml"); XmlNodeList nodeList = xmlDoc.SelectNodes("Books//Book//Price[@Unit=\"$\"]"); //遍历所有子节点 foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; xe.RemoveAttribute("Unit"); } xmlDoc.Save("Books.xml"); MessageBox.Show("数据已删除!"); } private XmlElement GetXmlElement(XmlDocument doc, string elementName, string value) { XmlElement element = doc.CreateElement(elementName); element.InnerText = value; return element; } } }
相关文章推荐
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- 使用XmlDocument创建XML文档及增加删除更新节点
- tinyxml使用:创建XML,遍历打印xml文件 select操作,获取单个节点值,删除节点操作,修改节点操作,增加节点操作
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- 用Java对xml文档进行遍历,更新,创建,删除
- GOOGLE东西时竟然找到了一个N年前写的东东:asp语言对XML文档中指定节点文本的增加、删除、修改、查看
- C#中通过XmlDocument操作Xml文档(新建XML、新增节点、删除节点、显示文档内容)
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例
- QtCreator 使用DOM创建和操作(添加,删除,更新)XML
- [ActionScript 3.0] AS3 对XML的操作,创建、删除、增加节点方法
- 使用jquery增加、删除html文档节点
- 这是一个关于XML文档的操作管理器XMLHelper类,类中包括XML文档的创建,文档节点和属性的读取,添加,修改,删除的方法功能的实现
- C#使用XmlDocument操作XML进行查询、增加、修改、删除、保存应用的实例(转载)
- C#使用linq to xml完成对XML文件的创建、增加、删除、修改、查询
- 详解PHP使用DOMDocument类遍历、增加、修改、删除XML节点操作