C#操做XML增刪改查

XML文件是一種經常使用的文件格式,無論是B/S仍是C/S都隨處可見XML的身影。Xml是Internet環境中跨平臺的,依賴於內容的技術,是當前處理結構化文檔信息的有力工具。XML是一種簡單的數據存儲語言,使用一系列簡單的標記描述數據,而這些標記能夠用方便的方式創建,雖然XML佔用的空間比二進制數據要佔用更多的空間,但XML極其簡單易於掌握和使用。微軟也提供了一系列類庫來倒幫助咱們在應用程序中存儲XML文件。java

    「在程序中訪問進而操做XML文件通常有兩種模型,分別是使用DOM(文檔對象模型)和流模型,使用DOM的好處在於它容許編輯和更新XML文檔,能夠隨機訪問文檔中的數據,可使用XPath查詢,可是,DOM的缺點在於它須要一次性的加載整個文檔到內存中,對於大型的文檔,這會形成資源問題。流模型很好的解決了這個問題,由於它對XML文件的訪問採用的是流的概念,也就是說,任什麼時候候在內存中只有當前節點,但它也有它的不足,它是隻讀的,僅向前的,不能在文檔中執行向後導航操做。」node

XML文件建立

首先來看一個簡單的XML文件:工具

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
  <Person id="1">
    <Name>FlyElephant</Name>
    <Age>24</Age>
  </Person>
  <Person id="2">
    <Name>keso</Name>
    <Age>25</Age>
  </Person>
</Persons>

 這是最多見的Dom形式的XML文件,建立的話也比較簡單,代碼以下:spa

     XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(dec);
            //根節點  
            XmlElement root = doc.CreateElement("Persons");
            doc.AppendChild(root);

            //根節點的添加獨立子節點  
            XmlElement person = doc.CreateElement("Person");
            person.SetAttribute("id", "1");
            person.AppendChild(getChildNode(doc, "Name", "FlyElephant"));
            person.AppendChild(getChildNode(doc, "Age", "24"));
            root.AppendChild(person);

            //根節點的添加獨立子節點  
            person = doc.CreateElement("Person");
            person.SetAttribute("id", "2");
            person.AppendChild(getChildNode(doc, "Name", "keso"));
            person.AppendChild(getChildNode(doc, "Age", "25"));
            root.AppendChild(person);

            doc.Save("person.xml");
            Console.WriteLine("建立成功");

 XML文件的讀取

C#中讀取XML有三種方式,XmlDocument,XmlTextReader,Linq to Xml,因爲本人經常使用的是XmlDocument方法,其餘的方法,有興趣的能夠本身嘗試一下,看下查詢的實現:xml

   XmlDocument doc = new XmlDocument();
            doc.Load("person.xml");    //加載Xml文件  
            XmlElement root = doc.DocumentElement;   //獲取根節點  
            XmlNodeList personNodes = root.GetElementsByTagName("Person"); //獲取Person子節點集合  
            foreach (XmlNode node in personNodes)
            {
                string id = ((XmlElement)node).GetAttribute("id");   //獲取Name屬性值  
                string name = ((XmlElement)node).GetElementsByTagName("Name")[0].InnerText;  //獲取Age子XmlElement集合  
                string age = ((XmlElement)node).GetElementsByTagName("Age")[0].InnerText;
                Console.WriteLine("編號:" + id + "姓名:" + name + "年齡:" + age);
            }

  結果以下:對象

XML添加

XML存放的是數據結果跟類類似,若是業務須要往裏面動態添加數據,這個時候也須要我的控制一下,代碼以下:blog

           XmlDocument doc = new XmlDocument();
            doc.Load("person.xml");
            XmlElement root = doc.DocumentElement;
            //根節點的添加獨立子節點  
            XmlElement person = doc.CreateElement("Person");
            person.SetAttribute("id", "3");
            person.AppendChild(getChildNode(doc, "Name", "Elephant"));
            person.AppendChild(getChildNode(doc, "Age", "23"));
            root.AppendChild(person);
            doc.Save("person.xml");
            Console.WriteLine("XML文件中節點添加成功");

 這個時候XML文件已經發生變化:內存

<?xml version="1.0" encoding="UTF-8"?>
<Persons>
  <Person id="1">
    <Name>FlyElephant修改</Name>
    <Age>24</Age>
  </Person>
  <Person id="2">
    <Name>keso</Name>
    <Age>25</Age>
  </Person>
  <Person id="3">
    <Name>Elephant</Name>
    <Age>23</Age>
  </Person>
</Persons>

 XML修改

修改其中的一個節點的話,最簡單的就是遍歷,遍歷的時候修改本身想要修改的元素:資源

   XmlDocument doc = new XmlDocument();
            doc.Load("person.xml");    //加載Xml文件  
            XmlElement root = doc.DocumentElement;   //獲取根節點  
            XmlNodeList personNodes = root.GetElementsByTagName("Person"); //獲取Person子節點集合
            foreach (XmlNode node in personNodes)
            {
                XmlElement ele = (XmlElement)node;
                if (ele.GetAttribute("id") == "3")
                {
                    XmlElement nameEle = (XmlElement)ele.GetElementsByTagName("Name")[0];
                    nameEle.InnerText = nameEle.InnerText + "修改";
                }
            }
            Console.WriteLine("節點修改爲功");
            doc.Save("person.xml");

  固然若是XML文件中內容不少的話,這種方式就顯得的不是那麼的合理,能夠修改一下一上代碼文檔

            XmlElement selectEle = (XmlElement)root.SelectSingleNode("/Persons/Person[@id='1']");
            XmlElement nameEle = (XmlElement)selectEle.GetElementsByTagName("Name")[0];
            nameEle.InnerText = nameEle.InnerText + "修改";

XML刪除

通過上面的操做,刪除節點就很簡單的,代碼以下:

            XmlDocument doc = new XmlDocument();
            doc.Load("person.xml");    //加載Xml文件  
            XmlElement root = doc.DocumentElement;   //獲取根節點  
            XmlNodeList personNodes = root.GetElementsByTagName("Person"); //獲取Person子節點集合  
            XmlNode selectNode =root.SelectSingleNode("/Persons/Person[@id='1']");
            root.RemoveChild(selectNode);
            Console.WriteLine("節點刪除成功");
            doc.Save("person.xml");

  週末看博客的都是強人,你們週末愉快~

相關文章
相關標籤/搜索