开发者

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

开发者 https://www.devze.com 2023-01-11 00:27 出处:网络
i am using t开发者_StackOverflowhe HTML Agility pack to convert <font size=\"1\">This is a test</font>

i am using t开发者_StackOverflowhe 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 ?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消