1. 程式人生 > >C#對XML的基本操作

C#對XML的基本操作

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

    namespace OperateXML
   {
       class Program
     {
         static void Main(string[] args)
         {
             try
             {
                 //xml檔案儲存路徑
                 string myXMLFilePath = "E:\\MyComputers.xml";
                 //生成xml檔案
                 GenerateXMLFile(myXMLFilePath);
                 //遍歷xml檔案的資訊
                 GetXMLInformation(myXMLFilePath);
                 //修改xml檔案的資訊
                 ModifyXmlInformation(myXMLFilePath);
                 //向xml檔案新增節點資訊
                 AddXmlInformation(myXMLFilePath);
                 //刪除指定節點資訊
                 DeleteXmlInformation(myXMLFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

         private static void GenerateXMLFile(string xmlFilePath)
         {
             try
             {
                 //初始化一個xml例項
                 XmlDocument myXmlDoc = new XmlDocument();
                 //建立xml的根節點
                 XmlElement rootElement = myXmlDoc.CreateElement("Computers");
                 //將根節點加入到xml檔案中(AppendChild)
                 myXmlDoc.AppendChild(rootElement);

                 //初始化第一層的第一個子節點
                 XmlElement firstLevelElement1 = myXmlDoc.CreateElement("Computer");
                 //填充第一層的第一個子節點的屬性值(SetAttribute)
                 firstLevelElement1.SetAttribute("ID", "11111111");
                 firstLevelElement1.SetAttribute("Description", "Made in China");
                 //將第一層的第一個子節點加入到根節點下
                 rootElement.AppendChild(firstLevelElement1);
                 //初始化第二層的第一個子節點
                 XmlElement secondLevelElement11 = myXmlDoc.CreateElement("name");
                 //填充第二層的第一個子節點的值(InnerText)
                 secondLevelElement11.InnerText = "Lenovo";
                 firstLevelElement1.AppendChild(secondLevelElement11);
                 XmlElement secondLevelElement12 = myXmlDoc.CreateElement("price");
                 secondLevelElement12.InnerText = "5000";
                 firstLevelElement1.AppendChild(secondLevelElement12);

 
                 XmlElement firstLevelElement2 = myXmlDoc.CreateElement("Computer");
                 firstLevelElement2.SetAttribute("ID", "2222222");
                 firstLevelElement2.SetAttribute("Description", "Made in USA");
                 rootElement.AppendChild(firstLevelElement2);
                 XmlElement secondLevelElement21 = myXmlDoc.CreateElement("name");
                 secondLevelElement21.InnerText = "IBM";
                 firstLevelElement2.AppendChild(secondLevelElement21);
                 XmlElement secondLevelElement22 = myXmlDoc.CreateElement("price");
                 secondLevelElement22.InnerText = "10000";
                 firstLevelElement2.AppendChild(secondLevelElement22);

                 //將xml檔案儲存到指定的路徑下
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

         private static void GetXMLInformation(string xmlFilePath)
         {
             try
             {
                 //初始化一個xml例項
                 XmlDocument myXmlDoc = new XmlDocument();
                 //載入xml檔案(引數為xml檔案的路徑)
                 myXmlDoc.Load(xmlFilePath);
                 //獲得第一個姓名匹配的節點(SelectSingleNode):此xml檔案的根節點
                 XmlNode rootNode = myXmlDoc.SelectSingleNode("Computers");
                 //分別獲得該節點的InnerXml和OuterXml資訊
                 string innerXmlInfo = rootNode.InnerXml.ToString();
                 string outerXmlInfo = rootNode.OuterXml.ToString();
                 //獲得該節點的子節點(即:該節點的第一層子節點)
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //獲得該節點的屬性集合
                     XmlAttributeCollection attributeCol = node.Attributes;
                     foreach (XmlAttribute attri in attributeCol)
                     {
                         //獲取屬性名稱與屬性值
                         string name = attri.Name;
                         string value = attri.Value;
                         Console.WriteLine("{0} = {1}", name, value);
                     }

                     //判斷此節點是否還有子節點
                     if (node.HasChildNodes)
                     {
                         //獲取該節點的第一個子節點
                         XmlNode secondLevelNode1 = node.FirstChild;
                         //獲取該節點的名字
                         string name = secondLevelNode1.Name;
                         //獲取該節點的值(即:InnerText)
                         string innerText = secondLevelNode1.InnerText;
                         Console.WriteLine("{0} = {1}", name, innerText);

                         //獲取該節點的第二個子節點(用陣列下標獲取)
                         XmlNode secondLevelNode2 = node.ChildNodes[1];
                         name = secondLevelNode2.Name;
                         innerText = secondLevelNode2.InnerText;
                         Console.WriteLine("{0} = {1}", name, innerText);
                     }
                 }
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

         private static void ModifyXmlInformation(string xmlFilePath)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 XmlNode rootNode = myXmlDoc.FirstChild;
                 XmlNodeList firstLevelNodeList = rootNode.ChildNodes;
                 foreach (XmlNode node in firstLevelNodeList)
                 {
                     //修改此節點的屬性值
                     if (node.Attributes["Description"].Value.Equals("Made in USA"))
                     {
                         node.Attributes["Description"].Value = "Made in HongKong";
                     }
                 }
                 //要想使對xml檔案所做的修改生效,必須執行以下Save方法
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }

         }

         private static void AddXmlInformation(string xmlFilePath)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 //新增一個帶有屬性的節點資訊
                 foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
                 {
                     XmlElement newElement = myXmlDoc.CreateElement("color");
                     newElement.InnerText = "black";
                     newElement.SetAttribute("IsMixed", "Yes");
                     node.AppendChild(newElement);
                 }
                 //儲存更改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }

         private static void DeleteXmlInformation(string xmlFilePath)
         {
             try
             {
                 XmlDocument myXmlDoc = new XmlDocument();
                 myXmlDoc.Load(xmlFilePath);
                 foreach (XmlNode node in myXmlDoc.FirstChild.ChildNodes)
                 {
                     //記錄該節點下的最後一個子節點(簡稱:最後子節點)
                     XmlNode lastNode = node.LastChild;
                     //刪除最後子節點下的左右子節點
                     lastNode.RemoveAll();
                     //刪除最後子節點
                     node.RemoveChild(lastNode);
                 }
                 //儲存對xml檔案所做的修改
                 myXmlDoc.Save(xmlFilePath);
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }
     }
 }