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扩展协议用于数据表单,可以用 ...
随机推荐
- linux自定义开机启动服务
转 http://www.cnblogs.com/jimeper/archive/2013/03/12/2955687.html 手工创建服务 1.在/etc/rc.d/init.d目录下创建shel ...
- 配置并学习微信JS-SDK(1)
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> 微信JS-SDK ...
- mac+eclipse+svn+maven经验总结(转)
1.Eclipse for Mac:http://mirrors.ustc.edu.cn/eclipse/technology/epp/downloads/release/indigo/SR2/ecl ...
- WPF布局容器综合展示
Border控件,以及几个重要要的属性:Background:背景的 Brush 对象BorderBrush:用来绘制边框BorderThickness: Border 边框的宽度,设置边框每一边的线 ...
- php mysql PDO使用
<?php $dbh = new PDO('mysql:host=localhost;dbname=access_control', 'root', ''); $dbh->setAttri ...
- 代理Delegate的小应用(代理日期控件和下拉框)
前言 在平时关于表格一类的的控件使用中,不可避免需要修改每个Item的值,通过在Item中嵌入不同的控件对编辑的内容进行限定,然而在表格的Item中插入的控件始终显示,当表格中item项很多的时候,会 ...
- QT获得所有系统环境变量(包括Linux和MAC的信息)
系统环境变量还是挺重要的,除了QStandardPaths(感觉都是文档类型的变量,QT4使用QDesktopServices),更有QProcessEnvironment(都是真正的系统变量): Q ...
- 企业门户(Portal)项目实施方略与开发指南
<企业门户(Portal)项目实施方略与开发指南> 基本信息 作者: 郑文平 丛书名: 企业大型应用集成丛书 出版社:电子工业出版社 ISBN:9787121211843 上架时间: ...
- 在eclipse中安装activiti插件
最近在学习activiti,先学习安装插件吧. 单击help->Install new Software 然后添加资源 name:activiti location:http://activit ...
- 【HDOJ】3367 Pseudoforest
并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...