溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#中XML基礎(chǔ)用法有哪些

發(fā)布時間:2021-12-18 12:58:50 來源:億速云 閱讀:138 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹C#中XML基礎(chǔ)用法有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

什么是XML?

XML:可擴展標記語言。

XML的作用:

純文本,兼容性強。

和HTML的區(qū)別:

xml: 主要用來處理、存儲數(shù)據(jù)。無規(guī)定標簽,可擴展。

html:對數(shù)據(jù)的顯示和描述。 語法標簽固定。

XML語法特點:

區(qū)分大小寫。

只能有一個根節(jié)點。

標簽成對出現(xiàn)。

屬性用雙引號。

沒有預(yù)定標簽,用什么寫什么

文檔聲明:<?xml version=".." encoding="...">

注釋: <!--   -->

CDATA: 原意文本 <![CDATA[..] ] >

xmldocument 操作:

class Program
    {
        static void Main(string[] args)
        {
            //實現(xiàn)xml的寫入
            //1、在內(nèi)存中構(gòu)建Dom對象
            XmlDocument xmlDoc = new XmlDocument();
            //增加文檔說明
            XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
            xmlDoc.AppendChild(xmlDeclaration);
            //增加根元素
            //  創(chuàng)建根元素
            XmlElement rootElement = xmlDoc.CreateElement("school");
            xmlDoc.AppendChild(rootElement);
            //3、增加子元素,接下來添加的子元素增加到rootElement節(jié)點下
            XmlElement xmlClassElement = xmlDoc.CreateElement("class");
            // 為class元素添加id屬性
            XmlAttribute attr = xmlDoc.CreateAttribute("id");
            attr.Value = "x01";
            xmlClassElement.Attributes.Append(attr);
            rootElement.AppendChild(xmlClassElement);
            //4、為class創(chuàng)建student節(jié)點。
            XmlElement xmlStudentElement = xmlDoc.CreateElement("student");
            // 為student元素添加sid 屬性.
            XmlAttribute studentAttr = xmlDoc.CreateAttribute("sid");
            studentAttr.Value = "s011";
            xmlStudentElement.Attributes.Append(studentAttr);
            xmlClassElement.AppendChild(xmlStudentElement);
            //student中增加name節(jié)點。
            XmlElement xmlNameElement = xmlDoc.CreateElement("name");
            xmlNameElement.InnerText = "天";
            xmlStudentElement.AppendChild(xmlNameElement);


            //2、將該Dom對象寫入xml文件中
            xmlDoc.Save("school.xml");
            Console.WriteLine("ok");
        }
    }

以上方法可以用循環(huán)寫入。

xdocument 操作。

class Program
    {
        static void Main(string[] args)
        {
            //  通過xdocument 寫入文件
            List<Person> list = new List<Person>();
            list.Add(new Person() { Name = "Sam", Age = 18 });
            list.Add(new Person() { Name = "Penny", Age = 20 });
            // 1、 創(chuàng)建Dom對象。
            XDocument xDoc = new XDocument();
            XDeclaration xDec = new XDeclaration("1.0", "utf-8", null);
            // 設(shè)置文檔定義
            xDoc.Declaration = xDec;
            //2、創(chuàng)建根節(jié)點
            XElement rootElement = new XElement("List");
            xDoc.Add(rootElement);
            //3、循環(huán)創(chuàng)建節(jié)點
            for (int i = 0; i < list.Count; i++)
            {
                XElement PersonElement = new XElement("Person");
                PersonElement.SetAttributeValue("id", (i + 1).ToString());

                PersonElement.SetElementValue("Name", list[i].Name);
                PersonElement.SetElementValue("Age", list[i].Age);
                rootElement.Add(PersonElement);
            }
            xDoc.Save("List.xml");
            Console.WriteLine("ok");
        }
    }
    class Person
    {
        public string Name { get; set; }
        public  int Age { get; set; }
    }
class Program
    {
        static void Main(string[] args)
        {
            //讀取XML文件。
            XDocument document = XDocument.Load("List.xml");
            XElement rootElement = document.Root;
            Console.WriteLine("訂購人:{0}",rootElement.Element("CustomerName").Value);
            foreach (var item in rootElement.Element("Items").Elements("OrderItem"))
            {
                Console.WriteLine("商品名稱:{0}",item.Attribute("Name").Value);
            }
            
        }  
    }

以上是“C#中XML基礎(chǔ)用法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

xml
AI