C#操做XML方法集合

C#操做XML方法集合

 

 

一 前言

先來了解下操做XML所涉及到的幾個類及之間的關係  若是你們發現少寫了一些經常使用的方法,麻煩在評論中指出,我必定會補上的!謝謝你們html

* 1 XMLElement 主要是針對節點的一些屬性進行操做
* 2 XMLDocument 主要是針對節點的CUID操做
* 3 XMLNode 爲抽象類,作爲以上兩類的基類,提供一些操做節點的方法node

 

 

清楚了以上的關係在操做XML時會更清晰一點post

 

二 具體操做(C#)

  如下會對Xml的結點與屬性作增 刪 改 查的操做也知足了實際工做中的大部分狀況 學習

先構造一棵XML樹以下,其中也涉及到了寫入xml文檔的操做編碼

 

複製代碼
 1         public void CreatXmlTree(string xmlPath)
 2         {
 3             XElement xElement = new XElement(
 4                 new XElement("BookStore",
 5                     new XElement("Book",
 6                         new XElement("Name", "C#入門", new XAttribute("BookName", "C#")),
 7                         new XElement("Author", "Martin", new XAttribute("Name", "Martin")),
 8                         new XElement("Adress", "上海"),
 9                         new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
10                         ),
11                     new XElement("Book",
12                         new XElement("Name", "WCF入門", new XAttribute("BookName", "WCF")),
13                         new XElement("Author", "Mary", new XAttribute("Name", "Mary")),
14                         new XElement("Adress", "北京"),
15                         new XElement("Date", DateTime.Now.ToString("yyyy-MM-dd"))
16                         )
17                         )
18                 );
19 
20             //須要指定編碼格式,不然在讀取時會拋:根級別上的數據無效。 第 1 行 位置 1異常
21             XmlWriterSettings settings = new XmlWriterSettings();
22             settings.Encoding = new UTF8Encoding(false);
23             settings.Indent = true;
24             XmlWriter xw = XmlWriter.Create(xmlPath,settings);
25             xElement.Save(xw);
26             //寫入文件
27             xw.Flush();
28             xw.Close();
29         }
複製代碼

 

而後獲得以下的XML樹url

複製代碼
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <BookStore>
 3     <Book>
 4         <Name BookName="C#">C#入門</Name>
 5         <Author Name="Martin">Martin</Author>
 6         <Date>2013-10-11</Date>
 7         <Adress>上海</Adress>
 8         <Date>2013-10-11</Date>
 9     </Book>
10     <Book>
11         <Name BookName="WCF">WCF入門</Name>
12         <Author Name="Mary">Mary</Author>
13         <Adress>北京</Adress>
14         <Date>2013-10-11</Date>
15     </Book>
16 </BookStore>
複製代碼

 

如下操做都是對生成的XML樹進行操做spa

2.1 新增節點與屬性.net

新增節點NewBook並增長屬性Name="WPF"code

複製代碼
 1         public void Create(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             
 6             var root = xmlDoc.DocumentElement;//取到根結點
 7             XmlNode newNode = xmlDoc.CreateNode("element", "NewBook", "");
 8             newNode.InnerText = "WPF";
 9 
10             //添加爲根元素的第一層子結點
11             root.AppendChild(newNode);
12             xmlDoc.Save(xmlPath);
13         }
複製代碼

 

 開篇有寫操做xml節點屬性主要用XmlElement對象因此取到結點後要轉類型 xml

複製代碼
 1         //屬性
 2         public void CreateAttribute(string xmlPath)
 3         {
 4             XmlDocument xmlDoc = new XmlDocument();
 5             xmlDoc.Load(xmlPath);
 6             var root = xmlDoc.DocumentElement;//取到根結點
 7             XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
 8             node.SetAttribute("Name", "WPF");
 9             xmlDoc.Save(xmlPath);
10 
11         }
複製代碼

 

 效果以下

 

2.2 刪除節點與屬性

複製代碼
 1         public void Delete(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             var root = xmlDoc.DocumentElement;//取到根結點
 6 
 7             var element = xmlDoc.SelectSingleNode("BookStore/NewBook");
 8             root.RemoveChild(element);
 9             xmlDoc.Save(xmlPath);
10         }
複製代碼

刪除屬性

複製代碼
 1         public void DeleteAttribute(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
 6             //移除指定屬性
 7             node.RemoveAttribute("Name");
 8             //移除當前節點全部屬性,不包括默認屬性
 9             //node.RemoveAllAttributes();
10             xmlDoc.Save(xmlPath);
11         }
複製代碼

 

2.3 修改節點與屬性

   xml的節點默認是不容許修改的,本文也就不作處理了

  修改屬性代碼以下

複製代碼
1         public void ModifyAttribute(string xmlPath)
2         {
3             XmlDocument xmlDoc = new XmlDocument();
4             xmlDoc.Load(xmlPath);
5             XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6             element.SetAttribute("Name", "Zhang");
7             xmlDoc.Save(xmlPath);
8         }
複製代碼

效果以下

2.4 獲取節點與屬性

複製代碼
 1         public void Select(string xmlPath)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             xmlDoc.Load(xmlPath);
 5             //取根結點
 6             var root = xmlDoc.DocumentElement;//取到根結點
 7             //取指定的單個結點
 8             XmlNode oldChild = xmlDoc.SelectSingleNode("BookStore/NewBook");
 9             
10             //取指定的結點的集合
11             XmlNodeList nodes = xmlDoc.SelectNodes("BookStore/NewBook");
12 
13             //取到全部的xml結點
14             XmlNodeList nodelist = xmlDoc.GetElementsByTagName("*");
15         }
複製代碼

屬性

複製代碼
1         public void SelectAttribute(string xmlPath)
2         {
3             XmlDocument xmlDoc = new XmlDocument();
4             xmlDoc.Load(xmlPath);
5             XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("BookStore/NewBook");
6             string name = element.GetAttribute("Name");
7             Console.WriteLine(name);
8         }
複製代碼

 

 

三  具體操做 (linq to XML)

 Linq to Xml 也沒什麼變化只操做對象改變了主要涉及的幾個對象以下   注:我並無用linq的語法去操做元素

XDocument:用於建立一個XML實例文檔

XElement:用於一些節點與節點屬性的基本操做

如下是對Xml的 一些簡單的操做

3.1 新增節點與屬性

複製代碼
1         public void Create(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             XElement xElement = xDoc.Element("BookStore");
5             xElement.Add(new XElement("Test", new XAttribute("Name", "Zery")));
6             xDoc.Save(xmlPath);
7         }
複製代碼

屬性

複製代碼
 1         public void CreateAttribute(string xmlPath)
 2         {
 3             XDocument xDoc = XDocument.Load(xmlPath);
 4             IEnumerable<XElement> xElement = xDoc.Element("BookStore").Elements("Book");
 5             foreach (var element in xElement)
 6             {
 7                 element.SetAttributeValue("Name", "Zery");
 8             }
 9             xDoc.Save(xmlPath);
10         }
複製代碼

 

3.2 刪除節點與屬性

複製代碼
1         public void Delete(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             XElement element = (XElement)xDoc.Element("BookStore").Element("Book");
5             element.Remove();
6             xDoc.Save(xmlPath);
7         }
複製代碼

屬性

複製代碼
1         public void DeleteAttribute(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             //不能跨級取節點
5             XElement element = xDoc.Element("BookStore").Element("Book").Element("Name");
6             element.Attribute("BookName").Remove();
7             xDoc.Save(xmlPath);
8         }
複製代碼

3.3 修改節點屬性

節點.net沒提供修改的方法本文也不作處理

修改屬性與新增實質是同一個方法

複製代碼
1         public void ModifyAttribute(string xmlPath)
2         {
3             XDocument xDoc = XDocument.Load(xmlPath);
4             XElement element = xDoc.Element("BookStore").Element("Book");
5             element.SetAttributeValue("BookName","ZeryTest");
6             xDoc.Save(xmlPath);
7         }
複製代碼

 

  本文轉自Zery-zhang,轉載是爲了更好的記錄,記錄是爲了更好地學習

相關文章
相關標籤/搜索