代码如下using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemXml;
namespace XMLDOMDemo
{
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
private void btnLoad_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
MessageBoxShow(xmlDocInnerXml);
}
//创建文档
private void btnCreate_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
//建立Xml的定义声明
XmlDeclaration dec = xmlDocCreateXmlDeclaration( GB null);
xmlDocAppendChild(dec);
//创建根节点
XmlElement root = xmlDocCreateElement(Books);
xmlDocAppendChild(root);
XmlNode book = xmlDocCreateElement(Book);
XmlElement title = xmlDocCreateElement(Title);
titleInnerText = SQL Server;
bookAppendChild(title);
XmlElement isbn = xmlDocCreateElement(ISBN);
isbnInnerText = ;
bookAppendChild(isbn);
XmlElement author = xmlDocCreateElement(Author);
authorInnerText = jia;
bookAppendChild(author);
XmlElement price = xmlDocCreateElement(Price);
priceInnerText = ;
priceSetAttribute(Unit ___FCKpd___quot;);
bookAppendChild(price);
rootAppendChild(book);
xmlDocSave(Booksxml);
}
private void btnInsert_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
XmlNode root = xmlDocSelectSingleNode(Books);
XmlElement book = xmlDocCreateElement(Book);
XmlElement title = xmlDocCreateElement(Title);
titleInnerText = XML;
bookAppendChild(title);
XmlElement isbn = xmlDocCreateElement(ISBN);
isbnInnerText = ;
bookAppendChild(isbn);
XmlElement author = xmlDocCreateElement(Author);
authorInnerText = snow;
bookAppendChild(author);
XmlElement price = xmlDocCreateElement(Price);
priceInnerText = ;
priceSetAttribute(Unit ___FCKpd___quot;);
bookAppendChild(price);
rootAppendChild(book);
xmlDocSave(Booksxml);
MessageBoxShow(数据已写入!);
}
private void btnUpdate_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
////Book[@Unit=\$\]
//获取Books节点的所有子节点
XmlNodeList nodeList = xmlDocSelectSingleNode(Books//Book)ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)xn;
if (xeName == Author)
{
xeInnerText = amandag;
}
if (xeGetAttribute(Unit) == ___FCKpd___quot;)
{
xeSetAttribute(Unit ¥);
}
}
xmlDocSave(Booksxml);
}
private void btnDelete_Click(object sender EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDocLoad(Booksxml);
XmlNodeList nodeList = xmlDocSelectSingleNode(Books//Book)ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
//将子节点类型转换为XmlElement类型
XmlElement xe = (XmlElement)xn;
if (xeName == Author)
{
xeRemoveAll();
}
if (xeGetAttribute(Unit) == ¥)
{
xeRemoveAttribute(Unit);
}
}
xmlDocSave(Booksxml);
}
}
}