问一linq to xml 问题

macpc 2010-11-01 03:48:37
我的xml如下:

<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:user-50665551771.blogs</id><updated>2010-10-31T10:20:48.620Z</updated><title type='text'>XXXX's Blogs</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.blogger.com/feeds/78578689618554454552/blogs'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/78578689618554454552/blogs'/><link rel='alternate' type='text/html' href='http://www.blogger.com/profile/78578689618554454552'/><author><name>XXXX</name><uri>http://www.blogger.com/profile/78578689618554454552</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:user-50665551771.blog-6589794563979856415</id><published>2010-01-29T12:05:59.905-08:00</published><updated>2010-10-31T03:06:35.061-07:00</updated><title type='text'>XXXX博客</title><summary type='html'></summary><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/78578689618554454552/blogs/6589794563979856415'/><link rel='alternate' type='text/html' href='http://xxxxcom.blogspot.com/'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://xxxxcom.blogspot.com/feeds/posts/default'/><link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.blogger.com/feeds/6589794563979856415/posts/default'/><link rel='http://schemas.google.com/blogger/2008#template' type='application/atom+xml' href='http://www.blogger.com/feeds/6589794563979856415/template'/><link rel='http://schemas.google.com/blogger/2008#settings' type='application/atom+xml' href='http://www.blogger.com/feeds/6589794563979856415/settings'/><author><name>XXXX</name><uri>http://www.blogger.com/profile/78578689618554454552</uri><email>noreply@blogger.com</email></author></entry></feed>


我想取出<link rel='alternate' type='text/html' href='http://xxxxcom.blogspot.com/'/>里的href值即:http://xxxxcom.blogspot.com/

我的linq:

XNamespace ad = "http://www.w3.org/2005/Atom";
var vxml3 = from sturl3 in root.Descendants(ad + "link")
where (string)sturl3.Attribute("rel") == @"alternate"
select sturl3;

这样取出的符合rel="alternate"有两条数据,我如何取得到第二条数据?
...全文
163 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
claymore1114 2010-11-01
  • 打赏
  • 举报
回复

XNamespace ad = "http://www.w3.org/2005/Atom";
var Query1 = root.Element(ad+"entry")
.Descendants(ad + "link")
.FirstOrDefault(l => l.Attribute("rel").Value == @"alternate");
yqyqyoyo 2010-11-01
  • 打赏
  • 举报
回复
楼上正解。。
q107770540 2010-11-01
  • 打赏
  • 举报
回复

var vxml3 = (from sturl3 in root.Descendants(ad + "link")
where (string)sturl3.Attribute("rel") == @"alternate"
select sturl3.Attribute("href").Value).LastOrDefault();
if (vxml3 != null)
{
Console.WriteLine(vxml3);
}

q107770540 2010-11-01
  • 打赏
  • 举报
回复
var vxml3 = (from sturl3 in root.Descendants(ad + "link")
where (string)sturl3.Attribute("rel") == @"alternate"
select sturl3).LastOrDefault();

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧