Remove whitespaces and newlines when parsing with HtmlAgilityPack

元气小坏坏 提交于 2020-01-03 12:36:46

问题


I tried to parse HTML with the HtmlAgilityPack in the following way:

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(xhtmlString);

Unfortunately the xhtmlString contains unnecessary whitespaces and newline characters, so the _text of htmlDoc now looks like this:

<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\t<head></head>\n\t<body>\n\n<p>Alle Auktionen<br /></p>\n\n\t</body>\n</html>

This is a problem for me when working with the child elements of the body.

What is the easiest way to remove these unnecessary characters?

Does the HtmlAgilityPack offer some kind of function for cleaning up HTML from newlines and tabs?


回答1:


This is the document indentation and not unnecessary whitespaces and newline characters.
I cant see how this could be a problem but cant you just replace the special characteres such as "\t", "\n" ?

Doing a fast search i found this Html Agility Pack: make code look neat
Maybe setting up some properties to false can be helpful



来源:https://stackoverflow.com/questions/8743344/remove-whitespaces-and-newlines-when-parsing-with-htmlagilitypack

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