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扩展协议用于数据表单,可以用 ...
随机推荐
- GDB源代码查找路径
在gdb程序的时候,有时候会发现源代码文件找不到,对于那些带调试信息的系统库或者第三方库,很多时候当你真正想gdb去追他源代码的时候你会发现gdb根本找不到这些源代码路径.这个时候有两种选择: [1] ...
- cookie : 存储数据
cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2.cookie的存储 ...
- python 过滤html方法
from HTMLParser import HTMLParser class MLStripper(HTMLParser): """ 过滤html方法 "&q ...
- Chain of Responsibility
比较经典的距离是请假申请(<大话设计模式>中的例子),请假是要逐级判断,只有级别到了才有权利审批,从构造上面其实"装饰"模式和"职责链"之间有相通的 ...
- Quartz1.8.5例子(九)
/* * Copyright 2005 - 2009 Terracotta, Inc. * * Licensed under the Apache License, Version 2.0 (the ...
- git实现版本回退
1. 首先查看自己的版本: ***:~/piaoshifu_object/epiao.piaoshifu.cn$ git log commit c8d5c67861d2d0e21856cc2b4f60 ...
- poj 2778 DNA Sequence AC自动机DP 矩阵优化
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
- tyvj 1153 间谍网络 tarjan有向图强连通
P1153 - 间谍网络 From ForeverBell Normal (OI)总时限:13s 内存限制:128MB 代码长度限制:64KB 描述 Description 由于外国 ...
- android的原理,为什么不需要手动关闭程序
转自android的原理,为什么不需要手动关闭程序 不用在意剩余内存的大小,其实很多人都是把使用其他系统的习惯带过来来了. Andoird大多应用没有退出的设计其实是有道理的,这和系统对进程的调度机制 ...
- Windows消息对Edit控件的处理
例如对windows发消息让文本选中. SendMessage(Text1.hwnd,EM_GETSEL,0,-1 ); EC_LEFTMARGIN(&H1) EC_USEFONTINF // ...