Linq to XML 读取XML 备忘笔记
本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html
1. demo.xml

demo.xml
<note>
<conf>
<to>infozero</to>
<from>lerroy</from>
<heading>测试信息</heading>
<body>第一条测试信息</body>
<title name="我的第一条消息">from myself</title>
</conf>
<conf>
<to>infozero@163.com</to>
<from>text</from>
<heading> 时刻提醒我 </heading>
<body>这是一条测试信息!</body>
<title name="我的第二条消息">from others</title>
</conf>
</note>

2.在 程序中引用以下命名空间:
using System;
using System.Linq;
using System.Xml.Linq;
3.读取方法如下:

main 函数入口
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("demo.xml");
var text = from t in doc.Descendants("conf") //定位到节点 (XML字符串中一定要有root根节点,否者读取不到。)
.Where(w => w.Element("to").Value.Contains('@')) //若要筛选就用上这个语句
select new
{
to = t.Element("to").Value,
froms = t.Element("from").Value,
head = t.Element("heading").Value,
body = t.Element("body").Value,
title = t.Element("title").Attribute("name").Value //注意此处用到 attribute
};
foreach (var a in text)
{
Console.WriteLine(a.to);
Console.WriteLine(a.froms);
Console.WriteLine(a.head);
Console.WriteLine(a.body);
Console.WriteLine(a.title);
}
Console.ReadKey();
}
}

结果:

小结: Linq to Xml 确实是个好东西 ,它极大地方便了我们的编程。
Linq to XML读取xml的方法
添加应用:using System.Xml.Linq;
读取xml最重要的要知道xml是那种写法,因为linq已经把读取方式封装的相对傻瓜了,要有问题一定xml的结构问题。
第一种写法:
<?xml version="1.0" encoding="utf-8" ?>
<feeds>
<feed>
<image>ddddddddd</image>
<link>fffffffff</link>
<title>ddddddddddddddddd</title>
</feed>
</feeds>
Linq语句:
XDocument xdoc= XDocument.Load(Server.MapPath("xxx.xml"));
var ad = from a in xdoc.Descendants("feed")
select new
{
image = a.Element("image").Value,
link = a.Element("link").Value,
title = a.Element("title").Value
};
foreach (var a in ad)
Response.Write(a.image);
第二种xml
<?xml version="1.0" encoding="utf-8" ?>
<BizADsList>
<adData aid="1" image="815.jpg" link="http://www.dizhitiao.com" title="测试1"/>
<adData aid="2" image="5815.jpg" link="http://www.dizhitiao.com" title="测试2"/> // BizADsList节点元素下多个相同的adData节点元素
</BizADsList>
Linq语句:
XDocument adList = XDocument.Load(Server.MapPath("Data.xml"));
var ad = from a in adList.Descendants("BizADsList").Elements("adData")
select new
{
image = a.Attribute("image").Value,
link = a.Attribute("link").Value,
title = a.Attribute("title").Value
};
string s = "";
foreach (var a in ad)
s += a.image;
别告诉我没看出区别来。总之一句话,对xml要先多些了解。
---------- 后续补充:-----
另外一种 XML 格式 的解析参见: http://www.cnblogs.com/portalsky/archive/2008/09/11/1289461.html
XML 增删改查: http://www.cnblogs.com/greatverve/archive/2010/07/13/linq-to-xml-one-begin.html
Linq to XML 读取XML 备忘笔记的更多相关文章
- C#基础巩固(3)-Linq To XML 读取XML
记录下一些读取XML的方法,以免到用的时候忘记了,还得花时间去找. 一.传统写法读取XML 现在我有一个XML文件如下: 现在我要查找名字为"王五"的这个人的 Id 和sex(性别 ...
- C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML
using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...
- Unity3d 新建xml 读取xml
在游戏开发中.Xml常常被用来作为技能配置.地图配置.人物动作配置等配置文件. Unity3d内置的Xml库让我们非常方便地就能够新建Xml和读取Xml. 以下是一个样例,新建了一个Xml文档.而且读 ...
- maven -- 学习笔记(二)之setting.xml配置说明(备忘)
setting.xml配置说明,learn from:http://pengqb.javaeye.com,http://blog.csdn.net/mypop/article/details/6146 ...
- xml 基础学习备忘
<?xml version="1.0" encoding="UTF-8"? standalone="yes"> 这里的encod ...
- servlet2.3/2.5/3.0/3.1的xml名称空间备忘
The web.xml is a configuration file to describe how a web application should be deployed. Here’re 5 ...
- Linq to xml 读取xml文件或xml字符串
XMLFile1.xml: XDocument Contacts = XDocument.Load("XMLFile1.xml"); //XElement Contacts = X ...
- Spring AspectJ的Execution表达式-备忘笔记
Aspectj切入点语法定义 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式 execu ...
- XMPP备忘笔记
xmpp中文翻译计划: http://wiki.jabbercn.org/%E9%A6%96%E9%A1%B5 XEP-0004: 数据表单 摘要: 本文定义了一个XMPP扩展协议用于数据表单,可以用 ...
随机推荐
- 让IE8兼容问题,参考文档bootstrap
问题:制作的页面基本没啥问题,只有IE8不好使 参考文档:bootstrap官网 方案一 方案二
- jquery插件-validate
1.引入js,css 下载地址:http://plugins.jquery.com/validate/ 2.设置验证规则:input的class添加以下验证属性 3.设置不符合规则的提示信息:添加da ...
- 自定义函数中的参数返回值 “-> (Int -> Int)”的问题
func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { + number } retur ...
- PKM(personal knowledge management)
内化 一般含义 一般上,当涉及道德行为时,内化是巩固和植入某人信念.态度和价值的长期过程,而这一过程的实现牵扯到精神分析或行为方法的慎重使用. 当改变道德行为时,一组新的信念.态度和价值代替或适应于所 ...
- 对c++服务端进行覆盖率统计
(1)首先需要为每个被测程序的所有编译文件增加选项,如果文件太多,这无疑是灾难,可利用spec文件达到目的 sed -i '$ a\export LD_PRELOAD=/usr/local/bin/c ...
- uploadify 自定义按钮样式
uploadify是一款不错的JQUERY上传插件,但是FLASH按钮的外挂往往跟我们网页的设计不太搭配.一开始我还试图反编译uploadify.swf来修改其外观,结果发现反编译为FLA后里面没有任 ...
- Contest20140906 ProblemC 菲波拉契数制 DP
C.菲波拉契数制时间:2s 内存:65536KB我们定义如下数列为菲波拉契数列: F (1) = 1 F (2) = 2 ...
- Book of Evil
Codeforces Round #196 (Div. 2) D:http://codeforces.com/contest/337/status/D 题意:给你一个树,然后树中有一m个点,求到这m个 ...
- 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)
题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...
- WPF Image控件中的ImageSource与Bitmap的互相转换
原文:WPF Image控件中的ImageSource与Bitmap的互相转换 1.从bitmap转换成ImageSource [DllImport("gdi32.dll", ...