[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)
主界面
随机推荐
- 【转载】使用SQL Server维护计划实现数据库定时自动备份
在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库.而备份数据库一般又是在凌晨时间基本没有数据库操作的时候进行,所以我们不可能要求管理员 每天守到晚上1点去备份数据库.要实现数据库的 ...
- win7安装office2007出错被中断-已经解决
觉得雨林木风win7系统本身的office2007不好,但不能卸载,用360强力删除工具,把整个安装的文件夹全部删除,重新用之前能够在另外xp和win7系统成功安装的破解版office2007,安装开 ...
- EasyUI form ajax submit到MVC后,在IE下提示下载内容的解决办法
问题描述: 项目环境为,.Net Mvc5+EF6……前端框架使用的是EasyUI v1.4.4. 在视图页面中,使用form的submit方法提交表单数据时,如果是使用IE的话,请求成功后IE会提示 ...
- 项目中Service层的写法
截取自项目中的一个service实现类,记录一下: base类 package com.bupt.auth.service.base; import javax.annotation.Resource ...
- struts2,hibernate4,spring3配置时问题汇总及解决办法
文章转载于wanglihu的博客,原文链接http://wanglihu.iteye.com/blog/1897718 1.java.lang.NoClassDefFoundError: org/ob ...
- HDU 5093 Battle ships(二分图最大匹配)
题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...
- CentOS6.4下使用默认的PDF文档阅读器出现乱码的解决方案
方法一:修改/etc/fonts/conf.d/49-sansserif.conf文件,如下: 1: <?xml version="1.0"?> 2: <!DOC ...
- JVM调优总结 -Xms -Xmx -Xmn -Xss(转载)
堆大小设置JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理内存限制.32位系统下,一般限制在1.5G~2G:64为操作 ...
- 使用JavaScript实现弹出层效果
声明 阅读本文需要有一定的HTML.CSS和JavaScript基础 设计 实现弹出层效果的思路非常简单:将待显示的内容先隐藏,在触发某种条件后(如点击按钮),将原本隐藏的内容显示出来. 实现 < ...
- Jquery操作Cookie取值错误的解决方法
使用JQuery操作cookie时 发生取的值不正确,结果发现cookie有四个不同的属性,分享下错误的原因及解决方法. 使用JQuery操作cookie时 发生取的值不正确的问题: 结果发现coo ...