<?xml version="1.0" encoding="utf-8" ?>
<items>
<item id="1">
<title>标题</title>
<content>内容</content>
<descript>描述</descript>
</item>
<item id="2">
<title>标题</title>
<content>内容</content>
<descript>描述</descript>
</item>
</items>
==============读取节点数据
XDocument doc = XDocument.Load(Server.MapPath("~/xmldoc/test.xml"));
var items = from item in doc.Descendants("item")
where item.Attribute("id").Value == "2"
select new
{
title=item.Element("title").Value,
content=item.Element("content").Value
};
foreach (var item in items)
{
this.Response.Write(item.title);
this.Response.Write("<br>");
this.Response.Write(item.content);
}
或者
XDocument doc = XDocument.Load(Server.MapPath("~/xmldoc/test.xml"));
var items = from item in doc.Elements("items").Elements("item") select item;
foreach (var et in items)
{
this.Response.Write(et.Element("title").Value);
this.Response.Write("<br>");
this.Response.Write(et.Element("content").Value);
}
其中items中数据可以绑定到DataSource中
=================
XDocument.Load 与 XElement.Load 的比较