[C#]Linq To Xml 实例操作- 转
http://blog.sina.com.cn/s/blog_6c762bb301010oi5.html
http://blog.xuite.net/cppbuilder/blog/9940157
在文件头先加入下面语句:
using System.Xml.Linq;
一、XML文件
假设XML文件内容如下:
<pitchers>
<pitcher>
<name>C Wang</name>
<wins></wins>
<team>NYY</team>
</pitcher>
<pitcher>
<name>R.Johnson</name>
<wins></wins>
<team>NYY</team>
</pitcher>
<pitcher>
<name>R.Halladay</name>
<wins></wins>
<team>TOR</team>
</pitcher>
</pitchers>
二、用C#生成上面的XML文件
XElement pitchers = new XElement("pitchers",
new XElement("pitcher",
new XElement("name", "C Wang"),
new XElement("wins", ),
new XElement("team", "NYY")
),
new XElement("pitcher",
new XElement("name", "R.Johnson"),
new XElement("wins", ),
new XElement("team", "NYY")
),
new XElement("pitcher",
new XElement("name", "R.Halladay"),
new XElement("wins", ),
new XElement("team", "TOR")
)
);
Console.WriteLine(pitchers);
三、新增
// 新增一筆資料至尾端
pitchers.Add(new XElement("pitcher",
new XElement("name", "J.Santana"),
new XElement("wins", ),
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了。
六、加入属性(Attributes)
XElement wang = new XElement("pitcher",
new XAttribute("throws", "Right"), // 加上 pitcher 的屬性 "右投"
new XElement("name", "C Wang"),
new XElement("wins", ),
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", ),
new XElement("team", "NYY"));
//add season attribute
wang.Element("wins").SetAttributeValue("season","");
Console.WriteLine(wang);
结果如下:
刪除屬性可以用 XAttribute 的 Remove 方法,或是用 SetAttributeValue 將屬性值設定為 null, 如下所示:
XElement wang = new XElement("pitcher", new XAttribute("throws", "Right"),
new XElement("name", "C Wang"),
new XElement("wins", ),
new XElement("team", "NYY"));
wang.Element("wins").SetAttributeValue("season","");
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", ),
new XElement("team", "NYY"));
// 取得 throws 屬性的值
Console.WriteLine("Throws : " + wang.Attribute("throws").Value);
结果如下:
六、删除属性
// 移除throws屬性
wang.Attribute("throws").Remove();
七、加上Namespace
若要加上 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", ),
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", ),
new XElement("team", "MIN")
);
Console.WriteLine(santana);
八、為屬性加上 Namespace
要讓屬性名稱也加上 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", ,
new XAttribute(XNamespace.Xmlns + "season", "")),
new XElement("team", "NYY")
);
Console.WriteLine(wang);
九、装载某个XML文件
使用 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'>x@bcb.tw</email>
<email provider='yahoo'>iapx_432@yahoo.com.tw</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 = ,
Team = "NYY"},
new Pitcher{
Name = "R.Johnson",
Wins = ,
Team = "NYY"},
new Pitcher{
Name = "R.Halladay",
Wins = ,
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></wins>
<team>NYY</team>
</pitcher>
<pitcher>
<name>R.Johnson</name>
<wins></wins>
<team>NYY</team>
</pitcher>
</pitchers>
二、从一个XML文件中取得胜利次数大于17的球员姓名
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")) >=
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="">R.Halladay</pitcher>
<pitcher wins="">R.Johnson</pitcher>
<pitcher wins="">C Wang</pitcher>
</wins17>
[C#]Linq To Xml 实例操作- 转的更多相关文章
- C#操作Xml:linq to xml操作XML
LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...
- linq to xml操作XML(转)
转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...
- Linq To Xml操作XML增删改查
对XML文件的操作在平时项目中经常要运用到,比如用于存放一些配置相关的内容:本文将简单运用Linq TO Xml对XML进行操作,主要讲解对XML的创建.加载.增加.查询.修改以及删除:重点在于类XD ...
- 4.Linq To Xml操作XML增删改查
转自https://www.cnblogs.com/wujy/p/3366812.html 对XML文件的操作在平时项目中经常要运用到,比如用于存放一些配置相关的内容:本文将简单运用Linq TO X ...
- LINQ系列:LINQ to XML操作
LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...
- Linq对XML的简单操作
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- Linq学习笔记---Linq to Xml操作
LINQ to XML的成员, 属性列表: 属性 说明 Document 获取此 XObject 的 XDocument EmptySequence 获取空的元素集合 FirstAttribut ...
- Linq to XML操作XML文件
LINQ的类型 在MSDN官方文件中,LINQ分为几种类型: . LINQ to Objects(或称LINQ to Collection),这是LINQ的基本功能,针对集合对象进行查询处理,包括基本 ...
- c#操作xml文件(XmlDocument,XmlTextReader,Linq To Xml)
主界面
随机推荐
- js 调用php代码
<?php $test = "var a = ".$_GET['test'].";"; ?> <mce:script type="t ...
- (转)在 Windows 上安装Rabbit MQ 指南
rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.他遵循Mozilla Public License开源协议.采用 Erlang 实现的工业级的消息队列(MQ)服务器. Ra ...
- Java线程面试题 Top 50(转载)
原文链接:http://www.importnew.com/12773.html 本文由 ImportNew - 李 广 翻译自 javarevisited.欢迎加入Java小组.转载请参见文章末尾的 ...
- frame和iframe区别
1.frame不能脱离frameSet单独使用,iframe可以: 2.frame不能放在body中:如下可以正常显示: <!--<body>--> <frameset ...
- SilverLight命名空间详解-新手入门
1.核心命名空间 1.xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"是silverlight的核 ...
- Spring MVC的启动过程
一.概述 下面一个基本的运用springMVC的的web.xml的配置,这里要注意两个地方,一个是ContextLoadListener,一个是DispatcherServlet.web容器正是通过这 ...
- 而在Jquery中则使用$.map()、$.each()来操作数组
首先是普通的数组(索引为整数的数组): //$.map(arr,fn); //对数组中的每个元素调用fn函数逐个进行处理,fn函数将处理返回最后得到的一个新的数组 var arr = [9, 8, 7 ...
- Ubuntu16.04.1 安装MyCat
Mycat是一个开源的分布式数据库系统,但是由于真正的数据库需要存储引擎,而Mycat并没有存储引擎,所以并不是完全意义的分布式数据库系统. 安装Java环境,配置全局环境变量 MyCAT是使用JAV ...
- Jquery插件收集
移动端滚动条插件iScroll.js http://www.cnblogs.com/starof/p/5215845.html http://www.codeceo.com/article/35-jq ...
- ADO.NET的五个主要对象
优秀文章链接:http://www.cnblogs.com/xianspace/archive/2009/02/21/1395307.html http://www.cnblogs.com/aito/ ...