使用HtmlAgilityPack更換節點引發奇怪的錯誤

c# html html-agility-pack parsing

我有一個網頁,顯示用戶可以編輯的表格。編輯完成後,我想將表保存為.html文件,以後我可以將其轉換為圖像。我通過覆蓋render方法來做到這一點。但是,我想從最終版本中刪除兩個按鈕和一個DropDownList,這樣我就可以自己獲取表格了。這是我目前正在嘗試的代碼:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");

        var doc = new HtmlDocument();
        doc.LoadHtml(output);

        var query = doc.DocumentNode.Descendants("select");
        foreach (var item in query.ToList())
        {
            var newNodeStr = "<div></div>";
            var newNode = HtmlNode.CreateNode(newNodeStr);
            item.ParentNode.ReplaceChild(newNode, item);
        }

        File.WriteAllText(currDir + "\\outputFile.html", output);

        writer.Write(renderedContent);
    }
}

我在另一篇SO 帖子中找到了關於用HtmlAgilityPack替換節點的解決方案:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");

        var doc = new HtmlDocument();
        doc.LoadHtml(output);

        var query = doc.DocumentNode.Descendants("select");
        foreach (var item in query.ToList())
        {
            var newNodeStr = "<div></div>";
            var newNode = HtmlNode.CreateNode(newNodeStr);
            item.ParentNode.ReplaceChild(newNode, item);
        }

        File.WriteAllText(currDir + "\\outputFile.html", output);

        writer.Write(renderedContent);
    }
}

這是我試圖改變的呈現HTML:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");

        var doc = new HtmlDocument();
        doc.LoadHtml(output);

        var query = doc.DocumentNode.Descendants("select");
        foreach (var item in query.ToList())
        {
            var newNodeStr = "<div></div>";
            var newNode = HtmlNode.CreateNode(newNodeStr);
            item.ParentNode.ReplaceChild(newNode, item);
        }

        File.WriteAllText(currDir + "\\outputFile.html", output);

        writer.Write(renderedContent);
    }
}

對Replace的兩次調用正在按預期工作並刪除按鈕。不過這一行:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");

        var doc = new HtmlDocument();
        doc.LoadHtml(output);

        var query = doc.DocumentNode.Descendants("select");
        foreach (var item in query.ToList())
        {
            var newNodeStr = "<div></div>";
            var newNode = HtmlNode.CreateNode(newNodeStr);
            item.ParentNode.ReplaceChild(newNode, item);
        }

        File.WriteAllText(currDir + "\\outputFile.html", output);

        writer.Write(renderedContent);
    }
}

拋出這個錯誤:

protected override void Render(HtmlTextWriter writer)
{
    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
    {
        base.Render(htmlwriter);
        string renderedContent = htmlwriter.InnerWriter.ToString();
        string output = renderedContent.Replace(@"<input type=""submit"" name=""viewReport"" value=""View Report"" id=""viewReport"" />", "");
        output = output.Replace(@"<input type=""submit"" name=""redoEdits"" value=""Redo Edits"" id=""redoEdits"" />", "");

        var doc = new HtmlDocument();
        doc.LoadHtml(output);

        var query = doc.DocumentNode.Descendants("select");
        foreach (var item in query.ToList())
        {
            var newNodeStr = "<div></div>";
            var newNode = HtmlNode.CreateNode(newNodeStr);
            item.ParentNode.ReplaceChild(newNode, item);
        }

        File.WriteAllText(currDir + "\\outputFile.html", output);

        writer.Write(renderedContent);
    }
}

任何建議表示讚賞。

問候。

一般承認的答案

您似乎在針對.Net或更低版本的項目中使用.Net 4.5版本的Agility Pack,您只需要將Dll的引用更改為為Framework版本編譯的引用或將項目更改為.Net 4.5 (如果您使用的是VS 2012)。




許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因