C# 對XML基本操做總結

 

C# 對XML基本操做包括讀取節點的數據,添加節點。讀取節點屬性,修改節點屬性等。具體以下:html

XML文件:文件在MyDocument文件夾下node

<?xml version="1.0" encoding="utf-8"?>
<PersonF xmlns="" Name="(test)work hard work smart!">
  <person Name="Person1">
    <ID>1</ID>
    <Name>XiaoA</Name>
    <Age>59</Age>
  </person>
  <person Name="Person2">
    <ID>2</ID>
    <Name>XiaoB</Name>
    <Age>29</Age>
  </person>
  <person Name="Person3">
    <ID>3</ID>
    <Name>XiaoC</Name>
    <Age>103</Age>
  </person>
  <person Name="Person4">
    <ID>4</ID>
    <Name>XiaoD</Name>
    <Age>59</Age>
  </person>
</PersonF>

  

Code:說明都在註釋裏。post

        public  void TestXML()
        {
            XmlDocument doc = new XmlDocument();
            string path = "http://www.cnblogs.com/MyDocument/Person.xml";
            try
            {

                //doc.Load(Server.MapPath()
                doc.Load(path);

                //一、讀取單個節點的數據
                XmlNode node = doc.SelectSingleNode("PersonF");

                //二、讀取多個節點的數據
                XmlNodeList nodeList1 = doc.SelectNodes("PersonF/person");

                //3.1 讀取具體節點的具體值 如:屬性爲Person2的第二個節點Name的InnerText
                XmlNodeList nodeList = doc.DocumentElement.GetElementsByTagName("person");
                foreach (XmlNode node2 in nodeList1) //固然也能用nodeList的值
                {
                    if (node2.Attributes["Name"].InnerText == "Person2")
                    {
                        Console.WriteLine(node2.ChildNodes[1].InnerText);

                    }
                }

                //3.2 讀取ID爲2所在的節點第二個子節點Name的InnerText
                XmlNode node3 = doc.SelectSingleNode("PersonF/person[ID=2]");
                string strNode3 = node3.ChildNodes[1].InnerText;

                //3.3利用下面的方法能夠找到ID爲2的節點
                XmlNodeList nodeList2 = doc.SelectNodes("//person//ID");
                XmlNode node4 = null;
                foreach (XmlNode node5 in nodeList2)
                {
                    if (node5.InnerText == "2")
                    {
                        node4 = node5;
                        break;
                    }
                }
                Console.WriteLine(node4.InnerText);

                //四、讀取節點的屬性
                string Name = node.Attributes["Name"].InnerText;

                //5 修改節點的屬性
                node.Attributes["Name"].InnerText = "work hard work smart!";
                doc.Save(path);

                //6 添加自定義的節點
                XmlTextReader reader = new XmlTextReader(path);
                XmlElement root = doc.DocumentElement;//獲取根節點
                XmlElement tagOuter = doc.CreateElement("person");
                //設置節點屬性
                tagOuter.SetAttribute("Name", "Person5");
                XmlElement tagIN_Name = doc.CreateElement("Name");
                XmlElement tagIN_ID = doc.CreateElement("ID");
                tagIN_Name.InnerText = "work hard work smart!";
                tagIN_ID.InnerText = "32";
                tagOuter.AppendChild(tagIN_Name);
                tagOuter.AppendChild(tagIN_ID);
                root.AppendChild(tagOuter);//添加tagOuter到XML文件的最後
                reader.Close();
                doc.Save(path);
            }
            catch (System.Exception e)
            {
                throw new Exception(e.Message);
            }
        }

  

下文是對C#對XML的具體操做xml

如何完成.Net下XML文檔的讀寫操做htm

C#操做xml SelectNodes,SelectSingleNode老是返回NULL 與 xPath 介紹blog

C#中用SelectSingleNode方法解析帶有多個命名空間的XML文件utf-8

 

  



原文連接:http://www.cnblogs.com/linlf03/archive/2011/10/10/2205896.html 文檔

相關文章
相關標籤/搜索