C#操作XML-創建-追加-帶屬性的讀寫(全)

by 劉壯 | 2016/04/25 23:14

你好,我曾經接過一個上海的上機面試題,涉及到xml。今天分享一下c#是如何操作xml的。

大綱:
1.創建XML並實現保存
2.向XML中添加屬性
3.向XML中追加內容
4.讀取XML文件
5.讀取帶屬性的XML文件,含有移除(有一句提到)

1.創建XML並實現保存
首先,我們要實現添加不含屬性的xml文檔。這裏給出一個類,和一個調用方法以及貼出效果圖(案例中我用的中文xml,希望不給你帶來觀看效果)

public static void CreateXML(string xmlName)
        {
            //通過代碼創建XML文檔
            //1、引用命名空間   System.Xml
            //2、創建一個 xml 文檔
            XmlDocument xml = new XmlDocument();
            //3、創建一行聲明信息,並添加到 xml 文檔頂部
            XmlDeclaration decl = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(decl);

            //4、創建根節點
            XmlElement rootEle = xml.CreateElement("人");
            xml.AppendChild(rootEle);
            //5、創建子結點|屬性:性別
            XmlElement childEle = xml.CreateElement("性別");
            rootEle.AppendChild(childEle);

            XmlElement c2Ele = xml.CreateElement("男");
            c2Ele.InnerText = "1";
            childEle.AppendChild(c2Ele);
            c2Ele = xml.CreateElement("女");
            c2Ele.InnerText = "0";
            childEle.AppendChild(c2Ele);


            //6、創建子節點|屬性:四肢
            childEle = xml.CreateElement("胳膊");
            rootEle.AppendChild(childEle);

            c2Ele = xml.CreateElement("右胳膊");
            c2Ele.InnerText = "一般";
            childEle.AppendChild(c2Ele);

            c2Ele = xml.CreateElement("左胳膊");
            c2Ele.InnerText = "一般";
            childEle.AppendChild(c2Ele);

            c2Ele = xml.CreateElement("左退");
            c2Ele.InnerText = "粗壯";
            childEle.AppendChild(c2Ele);

            c2Ele = xml.CreateElement("右腿");
            c2Ele.InnerText = "粗壯";
            childEle.AppendChild(c2Ele);

            xml.Save(xmlName);

        }

調用方法:

 CreateXML("People.xml");

效果圖:
這裏寫圖片描述

2.向XML中添加屬性

在上面的類中, 最後的xml.Save(xmlName);前添加下面的代碼

            //添加帶有屬性的節點
            childEle = xml.CreateElement("hair");
            childEle.SetAttribute("Color", "black");
            childEle.InnerText = "頭髮";
            rootEle.AppendChild(childEle);

運行,查看你的xml文檔,就會多出來一行:

  <hair Color="black" />

效果如圖:
這裏寫圖片描述

3.向XML中追加內容
創建一個控制檯程序:
Main函數如下:

     static void Main(string[] args)
        {
            //創XML建對象
            XmlDocument doc = new XmlDocument();
            //聲明根節點
            XmlElement books;
            //判斷文件是否存在
            if (File.Exists("Books.xml"))
            {
                //該文件存在
                //加載文件
                doc.Load("Books.xml");
                //獲得根節點
                books = doc.DocumentElement;

            }
            else//該文件不存在
            {
                //創建聲明
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                //創建根節點
                books = doc.CreateElement("Books");
                doc.AppendChild(books);
            }

            //開始正常寫入信息就可以了
            XmlElement book1 = doc.CreateElement("Book");
            books.AppendChild(book1);

            XmlElement name = doc.CreateElement("Name");
            name.InnerText = "大話西遊"+(new Random()).Next(0,1000);
            book1.AppendChild(name);

            doc.Save("Books.xml");
            Console.WriteLine("追加完成");
            Console.ReadKey();
        }

運行兩次,效果圖:
這裏寫圖片描述
4.讀取XML文件
這裏沒有詳細寫,重點在第5條。你可以找到答案

            //獲得子節點   返回數組
            XmlNodeList xnl =  books.ChildNodes;
            foreach (XmlNode  item in xnl)
            {
                Console.WriteLine(item.InnerXml+":"+ item.InnerText);
            }

5.讀取帶屬性的XML文件
創建一個控制檯程序,你不需要關心xml的創建等等,我寫好了,都在這裏Program.cs代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace _027創建XML文檔
{
    class Program
    {
        static void Main(string[] args)
        {

            CreateXML("People.xml");
            ReadXML("People.xml");
            Console.WriteLine("創建成功");
            Console.ReadKey();
        }
        public static void ReadXML(string filename)
        {
            //創建對象
            XmlDocument xml = new XmlDocument();
            //加載 xml 文件
            xml.Load(filename);
            //獲取根節點
            XmlElement rootEle = xml.DocumentElement;

            //獲取子節點集合

            XmlNodeList xnl = xml.SelectNodes("/人/胳膊/左胳膊");
            foreach (XmlNode item in xnl)
            {
                Console.WriteLine(item.Attributes["毛"].InnerText);// .Value);
                //Console.WriteLine(item.Attributes["Hair"].Value);
            }
            //5、移除
            //xnl.RemoveAll();
        }
        public static void CreateXML(string xmlName)
        {
            //通過代碼創建XML文檔
            //1、引用命名空間   System.Xml
            //2、創建一個 xml 文檔
            XmlDocument xml = new XmlDocument();
            //3、創建一行聲明信息,並添加到 xml 文檔頂部
            XmlDeclaration decl = xml.CreateXmlDeclaration("1.0", "utf-8", null);
            xml.AppendChild(decl);

            //4、創建根節點
            XmlElement rootEle = xml.CreateElement("人");
            xml.AppendChild(rootEle);
            //5、創建子結點|屬性:性別
            XmlElement childEle = xml.CreateElement("性別");
            rootEle.AppendChild(childEle);

            XmlElement c2Ele = xml.CreateElement("男");
            c2Ele.InnerText = "1";
            childEle.AppendChild(c2Ele);
            c2Ele = xml.CreateElement("女");
            c2Ele.InnerText = "0";
            childEle.AppendChild(c2Ele);


            //6、創建子節點|屬性:四肢
            childEle = xml.CreateElement("胳膊");
            rootEle.AppendChild(childEle);

            c2Ele = xml.CreateElement("右胳膊");
            c2Ele.InnerText = "一般";
            childEle.AppendChild(c2Ele);

            c2Ele = xml.CreateElement("左胳膊");
            c2Ele.SetAttribute("毛", "真密");
            c2Ele.InnerText = "一般";
            childEle.AppendChild(c2Ele);

            c2Ele = xml.CreateElement("左退");
            c2Ele.InnerText = "粗壯";
            childEle.AppendChild(c2Ele);

            c2Ele = xml.CreateElement("右腿");
            c2Ele.InnerText = "粗壯";
            childEle.AppendChild(c2Ele);

            //添加帶有屬性的節點
            childEle = xml.CreateElement("hair");
            childEle.SetAttribute("Color", "black");
            childEle.InnerText = "頭髮";
            rootEle.AppendChild(childEle);

            xml.Save(xmlName);

        }
    }
}

運行,你會得到:“真密”

結束!!!
Web前後端技術交流羣:
點擊鏈接加入羣【Web前後端技術①羣】:http://jq.qq.com/?_wv=1027&k=29uDHtR

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章