C# RSS 聚合,如何以编程方式格式化文本

标签 c# winforms formatting rss syndication

我正在尝试构建一个新闻提要应用程序,但在格式化结果时遇到问题。我确信问题只是缺乏经验。我遇到两个主要问题。第一个是项目.summary.text 在摘要后面拉出一堆链接。

我遇到的另一个问题是将所有标题加粗并更改其颜色。我还使用了富文本框,这可能不是表示数据的最佳方式。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel.Syndication;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ltest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string url = "  http://feeds.reuters.com/reuters/topNews";
            XmlReader reader = XmlReader.Create(url);
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            reader.Close();
            foreach (SyndicationItem item in feed.Items)
            {
                Fodder_Box.SelectionStart = "Title".Length;
                Fodder_Box.SelectionColor = Color.Red;
                Fodder_Box.SelectionFont = new Font("Arial", 20, FontStyle.Bold);
                Fodder_Box.AppendText("Title: " + item.Title.Text + Environment.NewLine + Environment.NewLine);
                Fodder_Box.SelectionStart = "Summary".Length;
                Fodder_Box.SelectionColor = Color.Black;
                Fodder_Box.SelectionFont = new Font("Arial", 20, FontStyle.Regular);
                Fodder_Box.AppendText("Date: " + item.PublishDate.ToString("yyyy/MM/dd H:MM:ss") + Environment.NewLine + Environment.NewLine);
                Fodder_Box.AppendText("Summary: " + item.Summary.Text + Environment.NewLine);
                Fodder_Box.AppendText("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + Environment.NewLine + Environment.NewLine);
            }
        }
    }
}

最佳答案

您可以使用对 HTML 更友好的控件(如 WebBrowser)来代替 RichTextBox。控制。然后您可以构建 HTML 字符串并使用 CSS 对其进行格式化。假设您将一个名为 webBrowser 的 WebBrowser 控件添加到表单中。然后,您可以将当前的 foreach 循环替换为如下内容:

var text = new StringBuilder();

foreach (SyndicationItem item in feed.Items)
{
    text.AppendFormat("<p style='font-family:arial;color:red;font-size:20;font-weight:bold;'>Title: {0}</p>", item.Title.Text);
    text.AppendLine("</br>");
    text.AppendLine("</br>");

    text.AppendFormat("<p style='font-family:arial;font-size:20;'>Date: {0:yyyy/MM/dd H:MM:ss}</p>", item.PublishDate);
    text.AppendLine("</br>");
    text.AppendLine("</br>");

    text.AppendFormat("<p style='font-family:arial;font-size:20;'>Summary: {0}</p>", item.Summary.Text);
    text.AppendLine("</br>");
    text.AppendLine("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    text.AppendLine("</br>");
    text.AppendLine("</br>");
}

webBrowser.DocumentText = text.ToString();

另请参阅How to: Add Web Browser Capabilities to a Windows Forms Application

关于C# RSS 聚合,如何以编程方式格式化文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410798/

相关文章:

C#:按下 Enter 键时更改 DropDownList 值

python - 黑色不支持 "Format Selection"命令 - VS Code 错误

c# - 用于显示图像的 Windows 窗体文本框

c# - 如何使用 XML 树的可用结构在 C# 中构建 XML 树

c# - 在 C# 中, "SELECT TOP 0 * FROM (/* ... */) s"与 ADO.NET 结合使用是否是确定 SELECT 语句中的列信息的好方法?

c# - EF6 ToListAsync() 取消长查询不起作用

c# - 调用 MessageBox 和 BeginInvoking 有什么区别?

Python Datetime 将 timedelta 舍入到最近的最大单位

java - 为什么我的字符串不是固定大小?

c# - 您如何检查字符串列表是否与您的短语中的所有单词匹配?