使用XmlDocument创建XML文档及增加删除更新节点

时间:2022-02-13 08:58:26

 

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;
}
}
}