| root.Children.Count == 0) ...

问题描述 投票:0回答:1
The issue was in this line:

we need to replace it with

            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;
        }

document.DocumentNode.InnerHtml试着从代码中手动建立HTML。 代码,建立HTML。

在这种情况下,在准备好的文档中,当我试图访问时,我得到了Null引用 document.DocumentNode.InnerHtml. 但InnerText反映的是实际的文本,一切都好吗?

万一我document.LoadHtml("")同样的html.InnerText(""),就会出现 "InnerText"。

<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

毫无例外地返回真正的html?
c# html html-agility-pack nullreferenceexception
1个回答
0
投票

parentNode.ChildNodes.Add(node);

尝试从代码中手动构建HTML。代码,构建html: public string ParseFromRoot(RootItem root) { if (root == null

© www.soinside.com 2019 - 2024. All rights reserved.