将一个已知的XML放到XDocument 对象当中使用LINQ 查询非常的容易,下面是一个例子。
查找XML文档中的所有PERSONA元素显示结果个数,并遍历输出其值
public
void
MyDescendants()
{
XDocument xdoc = XDocument.Load(Server.MapPath( " hamlet.xml " ));
// 查找元素名为PERSONA 的元素
var query = from people in xdoc.Descendants( " PERSONA " )
select people.Value;
// people 是符合条件元素结果集 而people.Value;就是这个结果集的所有元素的值的序列
Response.Write(query.Count() + " 个玩家被找到。 " );
Response.Write( " <p />他们是: <p /> " );
foreach (var item in query)
{
// 输出他们的值
Response.Write( item + " <br /> " );
}
}
{
XDocument xdoc = XDocument.Load(Server.MapPath( " hamlet.xml " ));
// 查找元素名为PERSONA 的元素
var query = from people in xdoc.Descendants( " PERSONA " )
select people.Value;
// people 是符合条件元素结果集 而people.Value;就是这个结果集的所有元素的值的序列
Response.Write(query.Count() + " 个玩家被找到。 " );
Response.Write( " <p />他们是: <p /> " );
foreach (var item in query)
{
// 输出他们的值
Response.Write( item + " <br /> " );
}
}
通过URL 找到一个网络上的RSS XML文档,并查询。
public
void
MyRSS()
{
// 通过下面的代码你可以看到一个简单的网页RSS阅读器
XDocument xdoc = XDocument.Load( @" http://www.cnblogs.com/TerryFeng/rss " );
// 找到channel元素集合
var query = from rssFeed in xdoc.Descendants( " channel " )
select new
{
Title = rssFeed.Element( " title " ).Value, // 获取其下的title元素的值组成新结果集匿名对象的属性
Description = rssFeed.Element( " description " ).Value, // 获取其下的description元素的值组成新结果集匿名对象的属性
Link = rssFeed.Element( " link " ).Value, // 获取其下的link元素的值组成新结果集匿名对象的属性
};
foreach (var item in query)
{
Response.Write( " 博客: " + item.Title + " <br /> " );
Response.Write( " 描述: " + item.Description + " <br /> " );
Response.Write( " 地址: " + item.Link + " <br /><p /> " );
}
// 第二个查询,查询文档中的item元素,每个项目就是一个博客随笔
var queryPosts = from myPosts in xdoc.Descendants( " item " )
select new
{
Title = myPosts.Element( " title " ).Value,
Published = DateTime.Parse( myPosts.Element( " pubDate " ).Value),
Description = myPosts.Element( " description " ).Value,
Url = myPosts.Element( " link " ).Value,
Comments = myPosts.Element( " comments " ).Value,
};
foreach (var item in queryPosts)
{
Response.Write( " 标题: " + item.Title + " <br /> " );
Response.Write( " 发布日期: " + item.Published + " <br /> " );
Response.Write( " 链接: " + item.Url + " <br /> " );
Response.Write( " 内容: " + item.Description + " <br /> " );
Response.Write( " 注释: " + item.Comments + " <br /><p /> " );
}
Console.ReadLine();
}
{
// 通过下面的代码你可以看到一个简单的网页RSS阅读器
XDocument xdoc = XDocument.Load( @" http://www.cnblogs.com/TerryFeng/rss " );
// 找到channel元素集合
var query = from rssFeed in xdoc.Descendants( " channel " )
select new
{
Title = rssFeed.Element( " title " ).Value, // 获取其下的title元素的值组成新结果集匿名对象的属性
Description = rssFeed.Element( " description " ).Value, // 获取其下的description元素的值组成新结果集匿名对象的属性
Link = rssFeed.Element( " link " ).Value, // 获取其下的link元素的值组成新结果集匿名对象的属性
};
foreach (var item in query)
{
Response.Write( " 博客: " + item.Title + " <br /> " );
Response.Write( " 描述: " + item.Description + " <br /> " );
Response.Write( " 地址: " + item.Link + " <br /><p /> " );
}
// 第二个查询,查询文档中的item元素,每个项目就是一个博客随笔
var queryPosts = from myPosts in xdoc.Descendants( " item " )
select new
{
Title = myPosts.Element( " title " ).Value,
Published = DateTime.Parse( myPosts.Element( " pubDate " ).Value),
Description = myPosts.Element( " description " ).Value,
Url = myPosts.Element( " link " ).Value,
Comments = myPosts.Element( " comments " ).Value,
};
foreach (var item in queryPosts)
{
Response.Write( " 标题: " + item.Title + " <br /> " );
Response.Write( " 发布日期: " + item.Published + " <br /> " );
Response.Write( " 链接: " + item.Url + " <br /> " );
Response.Write( " 内容: " + item.Description + " <br /> " );
Response.Write( " 注释: " + item.Comments + " <br /><p /> " );
}
Console.ReadLine();
}