c#對XML文檔的創(chuàng)建與增刪改查的示例代碼
更新時間:2020年07月23日 09:06:59 作者:滑豬小板
這篇文章主要介紹了c#對XML文檔的創(chuàng)建與增刪改查的示例代碼,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
一、創(chuàng)建的第一種方式
//1、創(chuàng)建一個XML文檔
XmlDocument doc = new XmlDocument();
//2、創(chuàng)建第一行描述信息
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
//3、將創(chuàng)建的第一行描述信息添加到文檔中
doc.AppendChild(dec);
//4、給文檔添加根節(jié)點
XmlElement Books = doc.CreateElement("Books");
doc.AppendChild(Books);
XmlElement Book = doc.CreateElement("Book");
Books.AppendChild(Book);
XmlElement name = doc.CreateElement("name");
name.InnerText = "水滸傳";
Book.AppendChild(name);
XmlElement author = doc.CreateElement("author");
author.InnerText = "匿名";
author.SetAttribute("name", "wjl");
author.SetAttribute("count", "30");
Book.AppendChild(author);
doc.Save("Book.xml");
Console.WriteLine("保存成功!");
Console.ReadKey();
創(chuàng)建結(jié)果如下:

二、創(chuàng)建的第二種方式
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>();
list.Add(new Student(1, "wjl1", 22, "男"));
list.Add(new Student(2, "wjl2", 21, "男"));
list.Add(new Student(3, "wjl3", 22, "男"));
list.Add(new Student(4, "wjl4", 24, "男"));
XmlDocument xmldoc = new XmlDocument();
XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmldoc.AppendChild(xmldec);
XmlElement person = xmldoc.CreateElement("Person");
xmldoc.AppendChild(person);
for (int i = 0; i < list.Count; i++)
{
XmlElement stu = xmldoc.CreateElement("student");
stu.SetAttribute("ID", list[i].Id.ToString());
person.AppendChild(stu);
XmlElement name = xmldoc.CreateElement("name");
XmlElement age = xmldoc.CreateElement("age");
name.InnerText = list[i].Name;
age.InnerText = list[i].Age.ToString();
stu.AppendChild(name);
stu.AppendChild(age);
}
xmldoc.Save("Student.xml");
Console.WriteLine("Student.xml");
Console.ReadKey();
}
}
class Student
{
int id;
string name;
int age;
string sex;
public Student(int id, string name, int age, string sex)
{
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Student()
{
}
}
創(chuàng)建結(jié)果如下:

三、對XML文件的添加
XmlDocument doc = new XmlDocument();
//首先判斷文件是否存在,如果存在則追加否則在創(chuàng)建一個
if (File.Exists("Student.xml"))
{
//加載
doc.Load("Student.xml");
//獲取根節(jié)點,給根節(jié)點添加子節(jié)點
XmlElement person = doc.DocumentElement;
XmlElement student = doc.CreateElement("student");
student.SetAttribute("ID", "1");
person.AppendChild(student);
XmlElement name = doc.CreateElement("name");
XmlElement age = doc.CreateElement("age");
name.InnerText = "zjs";
age.InnerText = "41";
student.AppendChild(name);
student.AppendChild(age);
}
else {
}
doc.Save("Student.xml");
Console.WriteLine("Student.xml 保存成功");
四、對XML文檔的查詢、修改、刪除
方法1:
文檔結(jié)構(gòu)為:

if (File.Exists("order.xml"))
{
doc.Load("order.xml");
// 獲取根節(jié)點
XmlElement orderElement = doc.DocumentElement;
XmlNodeList orderChildr = orderElement.ChildNodes;
foreach (XmlNode item in orderChildr)
{
Console.WriteLine("節(jié)點名稱:"+ item.Name + "節(jié)點的 InnerText :" + item.InnerText);
}
XmlElement orderitem = orderElement["Items"];
XmlNodeList itemlist = orderitem.ChildNodes;
foreach (XmlNode item in itemlist)
{
Console.WriteLine(item.Attributes["Name"].Value + " " + item.Attributes["Count"].Value);
}
}
else
{
Console.WriteLine("文件不存在!");
}
Console.ReadKey();
// doc.Save("Student.xml");
Console.WriteLine("Student.xml 保存成功");
方法2:
#region 使用XPath的方式來讀取XML文件
// 獲取文檔對象
XmlDocument doc = new XmlDocument();
doc.Load("order.xml");
//獲取根節(jié)點
XmlElement order = doc.DocumentElement;
// 獲取單個節(jié)點
//XmlNode xn = order.SelectSingleNode(@"/Order/CustomerName");
XmlNode xn = order.SelectSingleNode(@"/Order/Items/OrderItem[@Name='碼表']");
xn.Attributes["Count"].Value = "20"; // 修改
doc.Save("Order.xml");
Console.WriteLine(xn.Attributes["Count"].Value);
Console.ReadKey();
#endregion
刪除元素指定的特性:
xn.Attributes.RemoveNamedItem("Count"); //刪除元素指定的特性
刪除子節(jié)點:
XmlNode xn = order.SelectSingleNode(@"/Order/Items"); XmlNode xnchild = order.SelectSingleNode(@"/Order/Items/OrderItem[@Name = '雨衣']"); xn.RemoveChild(xnchild); //刪除指定的子節(jié)點
刪除當(dāng)前所有子節(jié)點:
xn.RemoveAll(); //刪除當(dāng)前節(jié)點的所有子節(jié)點
刪除當(dāng)前節(jié)點的所有特性:
xnchild.Attributes.RemoveAll();
以上就是c#對XML文檔的創(chuàng)建與增刪改查的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于c#對XML文檔的創(chuàng)建與增刪改查的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實現(xiàn)操作MySql數(shù)據(jù)層類MysqlHelper實例
這篇文章主要介紹了C#實現(xiàn)操作MySql數(shù)據(jù)層類MysqlHelper,實例分析了C#操作MySQL的常用技巧,并將其封裝入一個類中以方便調(diào)用,需要的朋友可以參考下2015-04-04

