试图从源代码中删除所有href。我不明白我做错了什么

时间:2015-03-21 22:41:06

标签: c# winforms linq html-agility-pack

我正在尝试从标记中的源代码中删除所有href并使用class ="链接formlink" 。我不明白我做错了什么。我在"链接"中得到了空白。

StreamReader sr = new StreamReader(webBrowser1.DocumentStream);
        string sourceCode = sr.ReadToEnd();
        sr.Close();
        //removing illegal path 

        string regexSearch = new string(Path.GetInvalidFileNameChars()) +        new string(Path.GetInvalidPathChars());
        Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
        sourceCode = r.Replace(sourceCode, "");

        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.LoadHtml(sourceCode);

        var links = htmlDoc.DocumentNode
                          .Descendants("a")
                          .Where(x => x.Attributes["class"] != null
                                   && x.Attributes["class"].Value == "linked formlink")
                           .Select(x => x.Attributes["href"].Value.ToString());

1 个答案:

答案 0 :(得分:2)

正则表达式删除括号加上html-agile-pack使用的其他必要字符来确定标记和类

删除它