7

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?

Pratik
  • 11,534
  • 22
  • 69
  • 99
magnattic
  • 12,638
  • 13
  • 62
  • 115

1 Answers1

2

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

Community
  • 1
  • 1
m.rufca
  • 2,558
  • 2
  • 19
  • 26
  • I could replace the special chars manually, but I'd rather extract the html (without intendation etc). If for example the newline characters are encoded differently because the user who inputs the html has another OS, I might run into trouble. – magnattic Jan 05 '12 at 14:08
  • .Replace(Environment.NewLine, text); Can work for UNIX and non-UNIX platforms but dont know about "\t" – m.rufca Jan 05 '12 at 14:24
  • @matheusrufca - atticae's concern about new lines is valid. He isn't talking about having a problem running his code cross platform, his concern is about manipulating html generated by another platform. – M.Babcock Jan 05 '12 at 15:26