【问题标题】:RSS feed- Using WriteTo to update an existing TextSyndicationContentRSS 提要 - 使用 WriteTo 更新现有的 TextSyndicationContent
【发布时间】:2021-03-23 13:49:40
【问题描述】:

我正在帮助an open-source project。在其中,我们使用 var feed = new SyndicationFeed(...){...}; 获取 a feed(使用 Chrome 打开),其中包含不同的文章标题及其名称。

After that 我可以使用foreach(var item in feed.Items){} 遍历提要,并在其中使用item.Title.Textitem.Authors[0].Name 获取标题和名称字符串。

问题是这些属性是只读的,但我希望能够在 Title 属性的末尾添加一些文本。因此,更新标题的唯一方法(因为它的类型为TextSyndicationContent)似乎是使用item.Title.WriteTo()

如何使用 WriteTo 更新现有 Title 的值而不创建新的 RSS Feed?另外,如何获取 outerElementName 和 outerElementNamespace?

我添加了该功能需要我输入的内容的屏幕截图。

outerElementName "title" 和outerElementNamespace "group 吗?如果是,我可以放入什么作为XMLWriter 来更新title 的值?

【问题讨论】:

    标签: c# asp.net .net .net-core rss


    【解决方案1】:

    可以在项目上设置Title 属性。

    创建一个TextSyndicationContent 的新实例,并用附加文本复制标题。

    //...
    
    string newTitleText = item.Title.Text + "Some extra text content";
    
    if (!string.IsNullOrWhiteSpace(item.Title.Type)) {
        string type = item.Title.Type == "text" ? "Plaintext" : item.Title.Type;
        TextSyndicationContentKind textKind = (TextSyndicationContentKind)
            Enum.Parse(typeof(TextSyndicationContentKind), type, ignoreCase: true);
        item.Title = new TextSyndicationContent(newTitleText, textKind);
    } else {
        item.Title = new TextSyndicationContent(newTitleText);
    }
    
    //...
    

    【讨论】:

    • 嘿@Nkosi,你太棒了,不敢相信我错过了!我尝试了您的解决方案,但我在此行 TextSyndicationContentKind type = (TextSyndicationContentKind)Enum.Parse(typeof(TextSyndicationContentKind), item.Title.Type, ignoreCase: true); 上收到 System.Argumentexception,说“未找到请求的值‘文本’。”任何想法为什么,我不明白“文本”来自哪里?
    • 好吧想通了。我试图使用构造函数枚举通过docs.microsoft.com/en-us/dotnet/api/… 重新创建类型,但需要为纯文本添加条件
    • @Saamer 修复了它。
    • 天哪,你真是个天才。你的 GitHub/linkedin/Twitter 是什么?我很想给你一个呐喊
    • @Saamer 很高兴为您提供帮助。我喜欢保持低调,所以不需要大喊大叫。不管怎样,谢谢你。编码愉快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多