http://blog.sina.com.cn/s/blog_6c762bb301010oi5.html
http://blog.xuite.net/cppbuilder/blog/9940157
在文件头先加入下面语句:
using System.Xml.Linq;
假设XML文件内容如下:
<pitchers> <pitcher> <name>C Wang</name> <wins>19</wins> <team>NYY</team> </pitcher> <pitcher> <name>R.Johnson</name> <wins>17</wins> <team>NYY</team> </pitcher> <pitcher> <name>R.Halladay</name> <wins>16</wins> <team>TOR</team> </pitcher> </pitchers>
XElement pitchers = new XElement("pitchers", new XElement("pitcher", new XElement("name", "C Wang"), new XElement("wins", 19), new XElement("team", "NYY") ), new XElement("pitcher", new XElement("name", "R.Johnson"), new XElement("wins", 17), new XElement("team", "NYY") ), new XElement("pitcher", new XElement("name", "R.Halladay"), new XElement("wins", 16), new XElement("team", "TOR") ) ); Console.WriteLine(pitchers);
// 新增一筆資料至尾端 pitchers.Add(new XElement("pitcher", new XElement("name", "J.Santana"), new XElement("wins", 19), new XElement("team", "MIN") ) );
增加结果 :J.Santana加在了最后面
// 移除第一筆資料(C Wang將被移除) pitchers.Element("pitcher").Remove();
执行Remove之后,结果如下:可以看到第一条被删除了。
// 將目前第一筆資料(R.Johnson)的 name 內容改成 cppbuilder pitchers.Element("pitcher").Element("name").SetValue("test");
替换结果如下:第一个name的内容提花为test了。
XElement wang = new XElement("pitcher", new XAttribute("throws", "Right"), // 加上 pitcher 的屬性 "右投" new XElement("name", "C Wang"), new XElement("wins", 19), new XElement("team", "NYY")); Console.WriteLine(wang);
结果如下:
修改屬性,用 SetAttributeValue ;
XElement wang = new XElement("pitcher", new XAttribute("throws", "Right"), new XElement("name", "C Wang"), new XElement("wins", 19), new XElement("team", "NYY")); //add season attribute wang.Element("wins").SetAttributeValue("season","2006"); Console.WriteLine(wang);
结果如下:
刪除屬性可以用 XAttribute 的 Remove 方法,或是用 SetAttributeValue 將屬性值設定為 null, 如下所示:
XElement wang = new XElement("pitcher", new XAttribute("throws", "Right"), new XElement("name", "C Wang"), new XElement("wins", 19), new XElement("team", "NYY")); wang.Element("wins").SetAttributeValue("season","2006"); wang.Element("wins").SetAttributeValue("season",null); Console.WriteLine(wang);
结果如下,可以看到season属性没有了。
XElement wang = new XElement("pitcher", new XAttribute("throws", "Right"), new XAttribute("bats", "Right"), new XElement("name", "C Wang"), new XElement("wins", 19), new XElement("team", "NYY")); // 取得 throws 屬性的值 Console.WriteLine("Throws : " + wang.Attribute("throws").Value);
结果如下:
// 移除throws屬性 wang.Attribute("throws").Remove();
若要加上 Namespace ,可以寫在大括號內,並放到元素名稱之前,例如 <element xmlns="http://bcb.tw" /> 可寫成 "{http://bcb.tw}element" ,也可以用 XNamespace 類別,其用法類似 string,如下:
XElement wang = new XElement("{http://bcb.tw}pitcher", new XElement("name", "C Wang"), new XElement("wins", 19), new XElement("team", "NYY") ); Console.WriteLine(wang); XNamespace ns = "http://bcb.tw"; // 注意這裡沒有使用 new, 而且不能改用 string XElement santana = new XElement(ns + "pitcher", new XElement("name", "J.Santana"), new XElement("wins", 19), new XElement("team", "MIN") ); Console.WriteLine(santana);
要讓屬性名稱也加上 Namespace ,例如 xmlns:p ,將屬性名稱寫成 "xmlns:p" 是錯誤的,必須用 XNamespace + "p"
X
Namespace ns = "http://bcb.tw"; XElement wang = new XElement(ns + "pitcher", new XAttribute(XNamespace.Xmlns + "throws", "Right"), // 不可寫成 new XElement("xmlns:throws", "Right"), new XElement("name", "C Wang"), new XElement("wins", 19, new XAttribute(XNamespace.Xmlns + "season", "2006")), new XElement("team", "NYY") ); Console.WriteLine(wang);
使用 XDocument 的 Load 方法可以讀入 XML 檔案,Save 可以儲存至 XML 檔案,注意 Load 是 static。"pitchers.xml" 內容同上。下面的示例是取出name元素内容:
XDocument xmlDoc = XDocument.Load("pitchers.xml"); XElement pitchers = xmlDoc.Root; foreach (XElement p in pitchers.Elements()) Console.WriteLine(p.Element("name").Value);
如果上面這範例改用列舉 Node ,有什麼差別呢?
Element 指的是一個元素,必須有標籤框住的才算元素,而節點則是會包含標籤外的文字。请看下面示例。
用同一元素来列巨额他的所有子元素和子节点,观察有何异同:
XElement cppbuilder = XElement.Parse( @"<contact>test content many contents so many contents <id>cppbuilder</id> <blogUrl>http://bcb.tw/blog</blogUrl> <email provider='x'>[email protected]</email> <email provider='yahoo'>[email protected]</email> </contact>"); Console.WriteLine("------ List Elements"); foreach (var c in cppbuilder.Elements()) Console.WriteLine(c); Console.WriteLine("------ List Nodes"); foreach (var c in cppbuilder.Nodes()) Console.WriteLine(c);
以上方法,只是增加了 XML 檔的存取便利性,如果要從一堆資料理篩選出想要的資料,用 Linq 是最快的了。
我们讲了XML to Linq的基本操作,现在我们讲到如何用Linq来操作XML。
首先建立一个class,用来记录球员姓名,胜利的次数,所属球队,如下:
class Pitcher { public string Name; public int Wins; public string Team; }
接着我们建立一个数组,如下:
var pitchers = new [] { new Pitcher{ Name = "C Wang", Wins = 19, Team = "NYY"}, new Pitcher{ Name = "R.Johnson", Wins = 17, Team = "NYY"}, new Pitcher{ Name = "R.Halladay", Wins = 16, Team = "TOR"} };
XElement pitchersXml = new XElement("pitchers", from p in pitchers where p.Team == "NYY" select new XElement("pitcher", new XElement("name", p.Name), new XElement("wins", p.Wins), new XElement("team", p.Team) ) ); Console.WriteLine(pitchersXml);
執行結果
<pitchers> <pitcher> <name>C Wang</name> <wins>19</wins> <team>NYY</team> </pitcher> <pitcher> <name>R.Johnson</name> <wins>17</wins> <team>NYY</team> </pitcher> </pitchers>
XDocument xmlDoc = XDocument.Load("pitchers.xml"); XElement pitchers = xmlDoc.Root; XElement wins17 = new XElement("wins17", from p in pitchers.Elements("pitcher") where int.Parse((string)p.Element("wins")) >= 17 select new object[] { new XElement("name", (string)p.Element("name")) }); Console.WriteLine(wins17);
執行結果
<wins17> <name>C Wang</name> <name>R.Johnson</name> </wins17>
XDocument xmlDoc = XDocument.Load("pitchers.xml"); XElement pitchers = xmlDoc.Root; XElement wins17 = new XElement("wins17", from p in pitchers.Elements("pitcher") orderby int.Parse((string)p.Element("wins")) select new object[] { new XElement("pitcher", (string)p.Element("name"), new XAttribute("wins", (string)p.Element("wins")) ) }); Console.WriteLine(wins17);
结果
<wins17> <pitcher wins="16">R.Halladay</pitcher> <pitcher wins="17">R.Johnson</pitcher> <pitcher wins="19">C Wang</pitcher> </wins17>