Linq to XML读取xml的方法
2008-09-11 21:37
393 查看
添加应用: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>
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最重要的要知道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>
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要先多些了解。
相关文章推荐
- Linq to xml 读取xml元素的方法
- Linq to Xml读取复杂xml(带命名空间)
- LINQ to XML CRUD,并且封装为DAL方法
- LinqTOXML DescendantNodes、DescendantNodesAndSelf和Descendants、DescendantsAndSelf方法
- C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串的方法
- Linq to xml读取属性
- 使用Linq to Xml 读取配置文件
- Linq to Xml读取复杂xml(带命名空间)
- linq to xml 中,有什么好的方法实现xml的字符编码吗?
- 读取xml通过deserialize和linq to sql快速更新数据表
- XML To Linq 读取Sharepoint列表中的附件列信息
- Linq to xml 读取xml文件
- Linq to Xml读取复杂xml(带命名空间)
- LinqToXml Ancestors和AncestorsAndSelf方法
- Linq to XML 读取XML 备忘笔记
- Linq to XML读取数据库生成XML
- 使用Linq to Xml 读取配置文件
- C# Linq To XML 读取XML,修改属性
- Linq to XML用XmlReader读取数据导致无法输出的格式化异常的问题
- C#基础巩固(3)-Linq To XML 读取XML