首页 > 代码库 > c# xml的增删改查
c# xml的增删改查
1.xml的用途很广,也很重要。
2.源代码直接分享
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;
using System.Collections;
namespace listbox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void CreateXmlFile(String FileName,String FistNode)
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明节点
XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmlDoc.AppendChild(node);
//创建根节点
XmlNode root = xmlDoc.CreateElement(FistNode);
xmlDoc.AppendChild(root);
try
{
xmlDoc.Save(FileName);
}
catch (Exception e)
{
//显示错误信息
Console.WriteLine(e.Message);
}
}
///<summary>
/// 插入节点
///</summary>
public void InsertNode(String nodename, String PrintPath, String Burnpath)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("TaskList.xml"); //加载xml文件
XmlNode root = xmlDoc.SelectSingleNode("TaskList");//查找﹤bookstore﹥
XmlElement xe1 = xmlDoc.CreateElement(nodename);//创建一个﹤book﹥节点
xe1.SetAttribute("PrintPath", PrintPath);//设置该节点genre属性
xe1.SetAttribute("BurnPath", Burnpath);//设置该节点ISBN属性
root.AppendChild(xe1);//添加到﹤bookshop﹥节点中
xmlDoc.Save("TaskList.xml"); //保存其更改
}
///<summary>
/// 修改节点的内容
///</summary>
public void UpdateNode(String nodename, String AttrName, String AttrVal)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("TaskList.xml"); //加载xml文件
XmlNodeList nodeList = xmlDoc.SelectSingleNode("TaskList").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
if (xn.Name == nodename)
{
XmlElement xe = (XmlElement)xn;
String val=xe.InnerText;
// xe.InnerText = "";
xe.SetAttribute(AttrName, AttrVal); //则修改该属性为“update Sky_Kwolf”
xmlDoc.Save("TaskList.xml");//保存。
break;
}
}
}
///<summary>
/// 读取节点的内容
///</summary>
private String ReadNodeAttr(String Nodename, String AttrName)
{
String result ="";
try
{
var doc = new XmlDocument();
doc.Load("TaskList.xml");
XmlNode root = doc.DocumentElement;
if (root != null)
{
XmlNodeList nodelist = root.SelectNodes(Nodename);
foreach (XmlNode node in nodelist)
{
try
{
if (AttrName == "PrintPath")
{
result = node.Attributes.Item(0).Value.ToString();
}
else
{
result = node.Attributes.Item(1).Value.ToString();
}
break;
}
catch (Exception fe)
{
}
}
}
}
catch (Exception ex)
{
}
return result;
}
///<summary>
/// 读取xml所有节点的内容
///</summary>
private String ReadAllNode()
{
String result = "";
try
{
var doc = new XmlDocument();
doc.Load("TaskList.xml");
XmlNode root = doc.DocumentElement;
if (root != null)
{
XmlNodeList nodelist = root.ChildNodes;
foreach (XmlNode node in nodelist)
{
result += node.Name + ‘,‘;
}
}
}
catch (Exception ex)
{
}
return result;
}
///<summary>
/// 删除节点的所有内容
///</summary>
public void DeleteNode(String nodename)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("TaskList.xml"); //加载xml文件
XmlNodeList nodeList = xmlDoc.SelectSingleNode("TaskList").ChildNodes;
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{
if (xn.Name == nodename)
{
XmlElement xe = (XmlElement)xn;
xe.RemoveAll();//删除该节点的全部内容
xmlDoc.SelectSingleNode("TaskList").RemoveChild(xn);
xmlDoc.Save("TaskList.xml");//保存。
break;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string peopleText = textBox1.Text.Trim().ToString();
//获取listbox1的对象
ListBox list1 = this.listBox1;
//判断人员是否已经添加过
if (!list1.Items.Contains(peopleText))
{
//list1.Items.Add(peopleText);
list1.Items.Insert(0, peopleText);
InsertNode(textBox1.Text, textBox2.Text, textBox3.Text);
}
else
{
MessageBox.Show("该人员已经添加过,无法重复添加!");
}
}
private void button2_Click(object sender, EventArgs e)
{
//获取listbox1的所有选中的项
if (this.listBox1.SelectedItems.Count > 0)
{
string checkPeople = this.listBox1.SelectedItem.ToString();
//判断是否添加到listbox2
if (!this.listBox2.Items.Contains(checkPeople))
{
//添加人员到listbox2中
this.listBox2.Items.Add(checkPeople);
//移除listbox1中
this.listBox1.Items.Remove(checkPeople);
}
else
{
MessageBox.Show("该人员已经转移过,无法重复转移!");
}
}
else
{
MessageBox.Show("未选中采购人员,无法转移销售部门!");
}
}
private void button3_Click(object sender, EventArgs e)
{
//获取listbox2的所有选中的项
if (this.listBox2.SelectedItems.Count > 0)
{
string checkPeople = this.listBox2.SelectedItem.ToString();
//判断是否添加到listbox1
if (!this.listBox1.Items.Contains(checkPeople))
{
//添加人员到listbox1中
this.listBox1.Items.Add(checkPeople);
//移除listbox1中
this.listBox2.Items.Remove(checkPeople);
}
else
{
MessageBox.Show("该人员已经转移过,无法重复转移!");
}
}
else
{
MessageBox.Show("未选中销售人员,无法转移到采购部门!");
}
}
private void button4_Click(object sender, EventArgs e)
{
CreateXmlFile("TaskList.xml", "TaskList");
InsertNode("f2", "p2", "b3");
//UpdateNode("f1", "PrintPath", "p3");
//UpdateNode("f1", "BurnPath", "b4");
// string p1= ReadNode("f2", "PrintPath");
// string p2 = ReadNode("f2", "BurnPath");
// string p1 = ReadNodeAttr("f1", "BurnPath");
//MessageBox.Show(p1);
//UpdateNode("f2", "PrintPath", "00");
//UpdateNode("f2", "BurnPath", "01");
//DeleteNode("f1");
// ReadAllNode();
}
private void Form1_Load(object sender, EventArgs e)
{
String name= ReadAllNode();
String[] name2 = name.Split(‘,‘);
for (int i = 0; i < name2.Length; i++)
{
listBox1.Items.Add(name2[i]);
}
}
}
}
c# xml的增删改查