C# 操作XML

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.