XmlDocument增删改查。
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 _02XmlDocumentReview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//xml文件储存路径
private static string xmlFilePath = Application.StartupPath + "\\ListComputer.xml";
//创建
private void button1_Click(object sender, EventArgs e)
{
GenerateXml(xmlFilePath);
MessageBox.Show("执行成功!");
}
private void GenerateXml(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
xmlDoc.AppendChild(xmlDec);
XmlElement root = xmlDoc.CreateElement("computers");
xmlDoc.AppendChild(root);
XmlElement firstElement1 = xmlDoc.CreateElement("computer");
firstElement1.SetAttribute("ID", "11111111");
firstElement1.SetAttribute("Description", "Made In China");
root.AppendChild(firstElement1);
XmlElement secondElement11 = xmlDoc.CreateElement("Name");
secondElement11.InnerText = "Lenovo";
firstElement1.AppendChild(secondElement11);
XmlElement secondElement12 = xmlDoc.CreateElement("Price");
secondElement12.InnerText = "5000";
firstElement1.AppendChild(secondElement12);
XmlElement firstElement2 = xmlDoc.CreateElement("computer");
firstElement2.SetAttribute("ID", "22222222");
firstElement2.SetAttribute("Description", "Made In USA");
root.AppendChild(firstElement2);
XmlElement secondElement21 = xmlDoc.CreateElement("Name");
secondElement21.InnerText = "IBM";
firstElement2.AppendChild(secondElement21);
XmlElement secondElement22 = xmlDoc.CreateElement("Price");
secondElement22.InnerText = "10000";
firstElement2.AppendChild(secondElement22);
xmlDoc.Save(xmlFilePath);
}
//遍历
private void button2_Click(object sender, EventArgs e)
{
GetXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
}
private void GetXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
//方法1:
XmlNode root = xmlDoc.DocumentElement;
//方法2:
XmlNode rootElement = xmlDoc.SelectSingleNode("computers");
foreach (XmlNode node in rootElement.ChildNodes)//root.ChildNodes)
{
string name = string.Empty;
string value = string.Empty;
//节点属性
foreach (XmlAttribute attri in node.Attributes)
{
name = attri.Name;
value = attri.Value;
MessageBox.Show(string.Format("{0}={1}", name, value));
}
if (node.HasChildNodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
name = node.ChildNodes[i].Name;
//value = node.ChildNodes[i].Value; //错
value = node.ChildNodes[i].InnerText;
MessageBox.Show(string.Format("{0}={1}", name, value));
}
}
}
}
//修改
private void button3_Click(object sender, EventArgs e)
{
UpdateXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
}
private void UpdateXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
if (node.Attributes["Description"].Value.Equals("Made In USA"))
{
node.Attributes["Description"].Value = "Made In HK";
}
if (node.HasChildNodes && node.Attributes["ID"].Value == "22222222")
{
node.ChildNodes[1].InnerText = "8000";
}
}
xmlDoc.Save(xmlFilePath);
}
//新增
private void button4_Click(object sender, EventArgs e)
{
AddXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
}
private void AddXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
XmlElement newElement = xmlDoc.CreateElement("Color");
newElement.SetAttribute("IsMixed", "Yes");
newElement.InnerText = "Black";
//xmlDoc.AppendChild(newElement);//错误
//root.AppendChild(newElement);//错误
//节点的级别要清晰
node.AppendChild(newElement);
}
xmlDoc.Save(xmlFilePath);
}
//删除
private void button5_Click(object sender, EventArgs e)
{
DeleteXmlFileInformation(xmlFilePath);
MessageBox.Show("执行成功!");
}
private void DeleteXmlFileInformation(string xmlFilePath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFilePath);
XmlNode root = xmlDoc.DocumentElement;
foreach (XmlNode node in root.ChildNodes)
{
XmlNode lastNode = node.LastChild;
lastNode.RemoveAll();
node.RemoveChild(lastNode);
}
xmlDoc.Save(xmlFilePath);
}
}
}