Search code examples
c#htmlhtml-agility-packnullreferenceexception

Manualy created HtmlDocument.DocumentNode returns null from InnerHtml


try to build HTML manually from code. code, that builds html:

            public string ParseFromRoot(RootItem root)
            {
                if (root == null || root.Children.Count == 0)
                    return string.Empty;

                var document = new HtmlDocument();

                foreach (var item in root.Children)
                    ParseNode(document, document.DocumentNode, item);

                return document.DocumentNode.InnerHtml;
            }

         private HtmlNode ParseNode(HtmlDocument document, HtmlNode parentNode, ItemBase item)
        {
            var node = DefineNode(document, item);

            parentNode.ChildNodes.Add(node);

            if (item.Children.Count == 0)
                return node;

            foreach (var child in item.Children)
                ParseNode(document, node, child);

            return node;
        }

in that case on the ready document, I've got Null reference when trying to access document.DocumentNode.InnerHtml. But InnerText reflects actual text, and all is okay?

In case I document.LoadHtml("") the same html, document.DocumentNode.InnerHtml returns real html without exception?

html:

<p>dsgwegsdrgsrdeg</p>
<p>&nbsp;</p>
<p>just <strong>typihere</strong></p>
<p>&nbsp;</p>
<ol>
   <li>some list</li>
   <li>another list</li>
</ol>
<p>j</p>

debug info


Solution

  • The issue was in this line:

    parentNode.ChildNodes.Add(node);
    

    we need to replace it with

    parentNode.AppendChild(node);