1. 程式人生 > >c# xml的增刪改查

c# xml的增刪改查

類型 window ttr eat val read 查找 element exc

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的增刪改查