Running into an issue trying to extract the text from a snippet of HTML

被刻印的时光 ゝ 提交于 2019-12-24 21:09:42

问题


i am using the HTML Agility pack to convert

 <font size="1">This is a test</font>

to

 This is a test

using this code:

 HtmlDocument doc = new HtmlDocument();
 doc.LoadHtml(html);
 string stripped = doc.DocumentNode.InnerText;

but i ran into an issue where i have this:

 <font size="1">This is a test &amp; this is a joke</font>

and the code above converted this to

This is a test &amp; this is a joke

but i wanted it to convert it to:

This is a test & this is a joke

does the html agility pack support what i am trying to do? why doesn't the HTML agiligy code do this by default or i am doing something wrong ?


回答1:


You can run HttpUtility.HtmlDecode() on the output.

However, note that InnerText will include HTML tags that may be contained inside the outermost tag. If you want to remove all tags, you will have to walk the document tree and retrieve all the text bit by bit.



来源:https://stackoverflow.com/questions/3425554/running-into-an-issue-trying-to-extract-the-text-from-a-snippet-of-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!