Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取网站上的所有RSS链接_C# - Fatal编程技术网

C# 获取网站上的所有RSS链接

C# 获取网站上的所有RSS链接,c#,C#,我目前正在编写一个非常基本的程序,它将首先通过网站的html代码查找所有RSS链接,然后将RSS链接放入一个数组,并将链接的每个内容解析为现有的XML文件 然而,我仍在学习C#,我还不太熟悉所有的课程。在PHP中,我使用get_file_contents()编写了自己的类,并使用cURL完成了这项工作。我也设法用Java解决了这个问题。无论如何,我试图通过使用C#来实现同样的结果,但我认为我在这里做了一些错误的事情 TLDR;编写正则表达式以查找网站上所有RSS链接的最佳方法是什么 到目前为止,

我目前正在编写一个非常基本的程序,它将首先通过网站的html代码查找所有RSS链接,然后将RSS链接放入一个数组,并将链接的每个内容解析为现有的XML文件

然而,我仍在学习C#,我还不太熟悉所有的课程。在PHP中,我使用get_file_contents()编写了自己的类,并使用cURL完成了这项工作。我也设法用Java解决了这个问题。无论如何,我试图通过使用C#来实现同样的结果,但我认为我在这里做了一些错误的事情

TLDR;编写正则表达式以查找网站上所有RSS链接的最佳方法是什么

到目前为止,我的代码如下所示:

        private List<string> getRSSLinks(string websiteUrl)
    {
        List<string> links = new List<string>();
        MatchCollection collection = Regex.Matches(websiteUrl, @"(<link.*?>.*?</link>)", RegexOptions.Singleline);

        foreach (Match singleMatch in collection)
        {
            string text = singleMatch.Groups[1].Value;
            Match matchRSSLink = Regex.Match(text, @"type=\""(application/rss+xml)\""", RegexOptions.Singleline);
            if (matchRSSLink.Success)
            {
                links.Add(text);
            }
        }

        return links;
    }
private List getRSSLinks(字符串网站URL)
{
列表链接=新列表();
MatchCollection collection=Regex.Matches(网站URL,@“(.*?”),RegexOptions.Singleline);
foreach(集合中的匹配singleMatch)
{
string text=singleMatch.Groups[1]。值;
Match matchRSSLink=Regex.Match(text,@“type=\”(application/rss+xml)\”,RegexOptions.Singleline);
如果(匹配链接成功)
{
添加链接(文本);
}
}
返回链接;
}

不要使用正则表达式解析html。使用html解析器,请参见以获取解释

我更喜欢解析HTML

using (var client = new WebClient())
{
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(client.DownloadString("http://www.xul.fr/en-xml-rss.html"));

    var rssLinks = doc.DocumentNode.Descendants("link")
        .Where(n => n.Attributes["type"] != null && n.Attributes["type"].Value == "application/rss+xml")
        .Select(n => n.Attributes["href"].Value)
        .ToArray();
}

谢谢!现在我已经完成了我想要的,多亏了你。。祝您有个美好的一天!