標籤:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Threading.Tasks;using System.Xml;using System.IO;namespace ConsoleApplication1{ /// <summary> /// XML操作類 /// </summary> public class Program { /// <summary> /// 入口方法 /// </summary> /// <param name="args">參數列表</param> public static void Main(string[] args) { string filePath="cars.xml"; CreateXmlFile(filePath); ReadXmlFile(filePath); ModifyXmlFile(filePath); DeleteXmlNode(filePath); } /// <summary> /// 建立一個XML檔案 /// </summary> /// <param name="filePath">檔案路徑</param> public static void CreateXmlFile(string filePath) { // 01.聲明XMLDocument XmlDocument doc = new XmlDocument(); // 02.向其中加入頭部聲明 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "utf-8", ""); doc.AppendChild(declaration); // 03.向其中加入子節點 XmlElement root = doc.CreateElement("cars"); root.SetAttribute("id", "10001");// 向結點中添加屬性 // 添加平治汽車資訊 XmlElement benz = doc.CreateElement("benz"); XmlElement benzName = doc.CreateElement("Name"); benzName.InnerText = "平治";//InnerText benz.AppendChild(benzName); XmlElement benzMaxSpeed = doc.CreateElement("MaxSpeed"); benzMaxSpeed.InnerText = "300"; benz.AppendChild(benzMaxSpeed); root.AppendChild(benz); // 添加寶馬汽車資訊 XmlElement bmw = doc.CreateElement("bmw"); bmw.InnerXml = "<Name>寶馬</Name><MaxSpeed>299</MaxSpeed>";//InnerXml root.AppendChild(bmw); // 04.儲存該檔案 doc.AppendChild(root); doc.Save(filePath); } /// <summary> /// 讀取Xml檔案 /// </summary> /// <param name="filePath">檔案路徑</param> public static void ReadXmlFile(string filePath) { // 第一種方法:使用XmlDocument.Load() if (File.Exists(filePath)) { // 01.載入XML文檔 XmlDocument doc = new XmlDocument(); doc.Load(filePath); // 02.讀取根節點及子節點 XmlNode root = doc.SelectSingleNode("cars"); if (root.HasChildNodes) { XmlNodeList xnd = root.ChildNodes; foreach (XmlNode xn in xnd) { XmlElement xe = (XmlElement)xn; Console.WriteLine(xn.InnerXml); } } } // 第二種方法:使用XmlDocument.LoadXml() if (File.Exists(filePath)) { using (StreamReader sr = new StreamReader(filePath)) { string xmlStr = sr.ReadToEnd(); XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlStr); // 以下代碼和上面一樣 } } // Load(),與LoadXml()的區別是: // 前者載入XML文檔 // 後者載入XML字串 } /// <summary> /// 修改Xml文檔資訊 /// </summary> /// <param name="filePath">文檔路徑</param> public static void ModifyXmlFile(string filePath) { if (File.Exists(filePath)) { // 01.載入Xml文檔 XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlElement root = (XmlElement)doc.SelectSingleNode("cars"); // 02.修改屬性資訊 root.SetAttribute("id","998"); // 03.修改結點資訊 XmlNode bwnMaxSpeed = (XmlElement)doc.SelectSingleNode("cars/bmw/MaxSpeed"); bwnMaxSpeed.InnerText = "319"; // 04.儲存文檔 doc.Save("mcar.xml"); } } /// <summary> /// 刪除結點或屬性資訊 /// </summary> /// <param name="filePath">Xml文檔路徑</param> public static void DeleteXmlNode(string filePath) { if (File.Exists(filePath)) { // 01.載入文檔 XmlDocument doc = new XmlDocument(); doc.Load(filePath); XmlElement root = (XmlElement)doc.SelectSingleNode("cars"); // 02.刪除屬性資訊 if (root.HasAttribute("id")) { root.RemoveAttribute("id"); } // 03.刪除結點資訊 XmlNodeList xnd = root.ChildNodes; foreach (XmlNode xn in xnd) { XmlNode subXn = xn.FirstChild; if (subXn.InnerText == "寶馬") { root.RemoveChild(xn); } } // 04.儲存更改 doc.Save("dcar.xml"); } } }}
C# 操作XML