You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中获取XML中rel为alternate的<link>标签的href值

问题:C#解析Atom XML时获取指定rel属性的标签href值

我正在用C#解析RSS/Atom XML,每个XML包含多个<entry>标签,单个<entry>的结构示例如下:

<entry>
    <id>232323232</id>
    <published>2020-03-09T00:00:00.000-04:00</published>
    <updated>2020-03-09T08:54:24.347-04:00</updated>
    <category scheme="http://www.blogger.com/atom/ns#" term="tour" />
    <title type='text'>LOre ipsum</title>
    <content type='html'>lorem ipsum
    </content>
    <link rel='replies' type='application/atom+xml'
        href='http://loremipsum.blog/comments/default'
        title='Post Comments' />
    <link rel='replies' type='text/html'
        href='http://loremipsum.blog/blog.html#comment-form'
        title='5 Comments' />
    <link rel='edit' type='application/atom+xml'
        href='http://loremipsum.blog/posts/default/4452478062189390152' />
    <link rel='self' type='application/atom+xml'
        href='http://loremipsum.blog/posts/default/4452478062189390152' />
    <link rel='alternate' type='text/html'
        href='http://loremipsum.blog/blog.html'
        title='Poison - Test Blog' />
    <author>
        <name>Bookish</name>
        <uri>http://loremipsum.blog/profile/12345</uri>
        <email>noreply@blogger.com</email>
        <gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32'
            src='http://2.bp.blogspot.com/something/bookishbutton.jpg' />
    </author>
    <media:thumbnail xmlns:media="http://search.yahoo.com/mrss/"
        url="https://1.bp.blogspot.com/something/poison.jpg"
        height="72" width="72" />
    <thr:total>5</thr:total>
</entry>

当前我的提取代码如下,但只能拿到第一个<link>标签的值,无法精准获取rel属性为alternate的<link>标签的href值(预期结果:http://loremipsum.blog/blog.html):

feeds = from feed in feedXML.Descendants("entry")
        select new
        {
            Title = feed.Element("title").Value,
            Link = feed.Element("link").Value, //<-- 此处需要修改
            Description = feed.Element("content").Value,
            PublishDate = feed.Element("published").Value
        };

解决方案

修改Link字段的获取逻辑,通过筛选<link>元素的rel属性定位目标标签,修改后的代码如下:

feeds = from feed in feedXML.Descendants("entry")
        select new
        {
            Title = feed.Element("title").Value,
            Link = feed.Elements("link")
                       .Where(l => (string)l.Attribute("rel") == "alternate")
                       .FirstOrDefault()?
                       .Attribute("href")?.Value,
            Description = feed.Element("content").Value,
            PublishDate = feed.Element("published").Value
        };

代码说明

  • feed.Elements("link"):获取当前<entry>节点下的所有<link>子元素
  • Where(l => (string)l.Attribute("rel") == "alternate"):筛选出rel属性值为alternate的<link>元素
  • FirstOrDefault():返回符合条件的第一个元素,若没有则返回null,避免空引用异常
  • ?.Attribute("href")?.Value:安全访问目标元素的href属性值,防止元素不存在时报错

内容的提问来源于stack exchange,提问作者Cyberpks

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:10:22