using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.XML;
namespace XMLDOMDemo
{
public partial class Form1 : Form
{
public Form1()
{
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)
{
XMLdocument XMLDoc = new XMLdocument();
//建立XML的定义声明
XMLDeclaration dec = XMLDoc.CreateXMLDeclaration("1.0", "GB2312", null);
XMLDoc.AppendChild(dec);
//创建根节点
XMLElement root = XMLDoc.CreateElement("Books");
XMLDoc.AppendChild(root);
XMLNode book = XMLDoc.CreateElement("Book");
XMLElement title = XMLDoc.CreateElement("Title");
title.InnerText = "SQL Server";
book.AppendChild(title);
XMLElement isbn = XMLDoc.CreateElement("ISBN");
isbn.InnerText = "444444";
book.AppendChild(isbn);
XMLElement author = XMLDoc.CreateElement("Author");
author.InnerText = "jia";
book.AppendChild(author);
XMLElement price = XMLDoc.CreateElement("Price");
price.InnerText = "120";
price.SetAttribute("Unit", "___FCKpd___0quot;);
book.AppendChild(price);
root.AppendChild(book);
XMLDoc.Save("Books.XML");
}
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 = XMLDoc.CreateElement("Title");
title.InnerText = "XML";
book.AppendChild(title);
XMLElement isbn = XMLDoc.CreateElement("ISBN");
isbn.InnerText = "333333";
book.AppendChild(isbn);
XMLElement author = XMLDoc.CreateElement("Author");
author.InnerText = "snow";
book.AppendChild(author);
XMLElement price = XMLDoc.CreateElement("Price");
price.InnerText = "120";
price.SetAttribute("Unit", "___FCKpd___0quot;);
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");
//"//Book[@Unit="$"]"
//获取Books节点的所有子节点
XMLNodeList nodeList = XMLDoc.SelectSingleNode("Books//Book").ChildNodes;
//遍历所有子节点
foreach (XMLNode xn in nodeList)
{
//将子节点类型转换为XMLElement类型
XMLElement xe = (XMLElement)xn;
if (xe.Name == "Author")
{
xe.InnerText = "amandag";
}
if (xe.GetAttribute("Unit") == "___FCKpd___0quot;)
{
xe.SetAttribute("Unit", "¥");
}
}
XMLDoc.Save("Books.XML");
}
private void btnDelete_Click(object sender, EventArgs e)
{
XMLdocument XMLDoc = new XMLdocument();
XMLDoc.Load("Books.XML");
XMLNodeList nodeList = XMLDoc.SelectSingleNode("Books//Book").ChildNodes;
//遍历所有子节点
foreach (XMLNode xn in nodeList)
{
//将子节点类型转换为XMLElement类型
XMLElement xe = (XMLElement)xn;
if (xe.Name == "Author")
{
xe.RemoveAll();
}
if (xe.GetAttribute("Unit") == "¥")
{
xe.RemoveAttribute("Unit");
}
}
XMLDoc.Save("Books.XML");
}
}
}