Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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# 提取段落标记中的内容_C#_Html_Webclient - Fatal编程技术网

C# 提取段落标记中的内容

C# 提取段落标记中的内容,c#,html,webclient,C#,Html,Webclient,我有以下html字符串,我必须提取的内容只在段落标签任何想法 链接是 我试过了 const string HTML_TAG_PATTERN = "<[^>]+.*?>"; static string StripHTML(string inputString) { return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);

我有以下html字符串,我必须提取的内容只在段落标签任何想法

链接是

我试过了

  const string HTML_TAG_PATTERN = "<[^>]+.*?>";
    static string StripHTML(string inputString)
            {
                return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);
            }
const string HTML_TAG_PATTERN=“]+.*?>”;
静态字符串StripHTML(字符串输入字符串)
{
返回Regex.Replace(inputString,HTML\u TAG\u PATTERN,string.Empty);
}
它删除了所有的html标记,但我不想删除所有的标记,因为这是我如何获得像逐段标记这样的内容的方法

其次,它在文本中对\n进行换行,而应用replace(“\n,”)则没有帮助 一个问题是当我申请时

int UrlStart = e.Result.IndexOf("<p>"), urlEnd = e.Result.IndexOf("<p>&nbsp;</p></td>\r" );
     string paragraph = e.Result.Substring(UrlStart, urlEnd);
     extractedContent.Text = paragraph.Replace(Environment.NewLine, "");
int-UrlStart=e.Result.IndexOf(“”),urlEnd=e.Result.IndexOf(“”);
字符串段落=e.Result.Substring(UrlStart,urlEnd);
extractedContent.Text=段落.Replace(Environment.NewLine,“”);

\r
这显示在段落末尾,但urlEnd不能确保只显示段落

提取的字符串在VisualStudio中显示如下 此页面由Webclient下载 HTMLpage结束

We will provide ourselves with ropes of\rsuitable length and strength- and- pardon me- you must not\rdrink more to-night.  our hands and feet must be steady and\rfirm tomorrow.\"\r<p>&nbsp;</p>     </td>\r    </tr>\r\r    <tr>\r     <td height=\"25\" width=\"10%\">\r     \r     </td><td height=\"25\" width=\"80%\" align=\"center\">\r       <font color=\"#FFFFFF\">\r       <font size=\"4\">1</font> &nbsp;\r       </font></td>\r     <td height=\"25\" width=\"10%\" align=\"right\"><a href=\"C2P1.shtml\">Next</a></td>\r    </tr>\r   </table>\r  </center>\r</div>\r<p align=\"center\"><a href=\"index.shtml\"><b>The Coming Race -by- Edward Bulwer Lytton</b></a></p>\r<P><B><center><A HREF=\"http://www.public-domain-content.com/encyclopedia.shtml\">Encyclopedia</a> - <A HREF=\"http://www.public-domain-content.com/books.shtml\">Books</a> - <A HREF=\"http://www.public-domain-content.com/religion.shtml\">Religion<a/> - <A HREF=\"http://www.public-domain-content.com/links2.shtml\">Links</a> - <A HREF=\"http://www.public-domain-content.com/\">Home</a> - <A HREF=\"http://www.webmaster-headquarters.com/mb/\">Message Boards</a></B><BR>This <a HREF=\"http://www.wikipedia.org/\">Wikipedia</a> content is licensed under the <a href=\"http://www.gnu.org/copyleft/fdl.html\">GNU Fr
我们将为自己提供长度和强度合适的绳索-请原谅-您今晚不能再滑冰了。我们的手和脚明天必须稳固。\“\r

\r\r\r\r\r\r\r\r\r\r\r\r

\r-
此内容根据GNU Fr授权
不要使用正则表达式来解析HTML。请改用(或类似的东西)

举个简单的例子,但您可以这样做:

HtmlDocument document = new HtmlDocument();
document.Load("your_file_here.htm");
foreach(HtmlNode paragraph in document.DocumentElement.SelectNodes("//p"))
{
    // do something with the paragraph node here
    string content = paragraph.InnerText; // or something similar
}

为什么使用正则表达式解析HTML是不好的-@Oded谢谢,我正要自己去挖掘那个URL:)无论如何,这并不是提取段落properly@Afnan当然,这只是一个快速的一次性示例,演示了使用基于DOM的解决方案而不是使用正则表达式的解决方案的简单性ut与
SelectNodes
函数一起使用的正确XPATH查询,以及您需要访问的
HtmlNode
的正确属性。我不知道有什么问题,但这不适用于我提供的网站。我已经分析了我显示的网站内容,其中包含大量内容,\r请注意更新问题中的pict他\r\n是否在每一个导致问题的地方