15

I just wanted to create my own simple document using the agility pack so create a new HtmlDocument that contains just the basic container elements - i.e.

<html><head></head><body></body></html>

How can I do this from scratch without actually loading the htmldocument with anything.

Pittfall
  • 2,751
  • 6
  • 32
  • 61
  • You should also tag this with .net - and your first sentence seems incomplete. Without knowing the agility pack more than just qick-googling it a minute ago, I suppose you'll have to start with a minimal document. – Fildor Jan 11 '12 at 16:37
  • 1
    I figured it out `HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlNode htmlNode = htmlDocument.CreateElement("html"); HtmlAgilityPack.HtmlNode headNOde = htmlDocument.CreateElement("head"); HtmlAgilityPack.HtmlNode bodyNode = htmlDocument.CreateElement("body"); htmlNode.AppendChild(headNode); htmlNode.AppendChild(bodyNode); htmlDocument.DocumentNode.AppendChild(htmlNode);` – Pittfall Jan 11 '12 at 17:08
  • @Pittfall `DocumentNode` is null in this case. – Neo Sep 27 '19 at 13:51

1 Answers1

27

Even easier:

var doc = new HtmlDocument();
var node = HtmlNode.CreateNode("<html><head></head><body></body></html>");
doc.DocumentNode.AppendChild(node);
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272