【问题标题】:Having problems with inserting node in a specific order using HTMLAgilityPack使用 HTMLAgilityPack 按特定顺序插入节点时出现问题
【发布时间】:2014-08-07 01:22:34
【问题描述】:

我再次迷失了 HTMLAgility。

这是我正在使用的 HTML 字符串:

<table>...</table>

我正在尝试通过添加以下内容来纠正此问题:

<html>
   <head>
   ...
   </head>
   <body>
      <table>
      ...
      </table>
   </body>
</html>

这是我的代码,我可以获取除正文之外的所有内容。 有什么建议吗?

HtmlNode htmlNode = doc.DocumentNode.SelectSingleNode("//html");
if (htmlNode == null)
{
    htmlNode = doc.CreateElement("html");
    HtmlNodeCollection htmlCollection = doc.DocumentNode.ChildNodes;
    htmlNode.AppendChildren(htmlCollection);
    doc.DocumentNode.RemoveAllChildren();
    doc.DocumentNode.PrependChild(htmlNode);
}

//check if <head> exists, if not create <head>
HtmlNode head = doc.DocumentNode.SelectSingleNode("//head");
if (head == null)
{
    head = doc.CreateElement("head");
    HtmlNodeCollection htmlCollection = doc.DocumentNode.ChildNodes;
    htmlNode.PrependChild(head);
}

HtmlNode cssLink = doc.DocumentNode.SelectSingleNode("//link[contains(@href, " + Url.Content("/assets/global/css/reset.css") + ")]");
if (cssLink == null)
{
    cssLink = doc.CreateElement("link");
    cssLink.SetAttributeValue("rel", "stylesheet");
    cssLink.SetAttributeValue("href", Url.Content("/assets/global/css/reset.css"));
    head.AppendChild(cssLink);
}

//check if <body> exists, if yes, add style='margin:0; padding:0'
HtmlNode htmlBody = doc.DocumentNode.SelectSingleNode("//body");
if (htmlBody == null)
{
    head = doc.DocumentNode.SelectSingleNode("//head");
    htmlBody = htmlNode.CloneNode("body", true);
    htmlNode.ChildNodes.Clear();
    htmlNode.AppendChild(htmlBody);
    //HtmlNodeCollection htmlCollection = doc.DocumentNode.ChildNodes;
    //htmlBody.AppendChildren(htmlCollection);
    //doc.DocumentNode.RemoveAllChildren();
    //doc.InsertBefore(htmlBody);
    //head.DocumentNode.AppendChild(htmlBody);
    //htmlNode.PrependChild(htmlBody);
}

这段代码给了我这个——你可以看到&lt;body&gt; 放错了地方。

<html>
   <body>
      <head>
      ...
      </head>
      <table>
      ...
      </table>
  </body>
</html>

【问题讨论】:

    标签: c# asp.net-mvc-4 html-agility-pack nodes


    【解决方案1】:

    您可以尝试在添加&lt;head&gt; 之前先添加&lt;body&gt; 节点,因为您似乎希望将除&lt;head&gt; 之外的所有&lt;html&gt; 内容放在&lt;body&gt; 标记内:

    .....
    HtmlNode htmlBody = doc.DocumentNode.SelectSingleNode("//body");
    if (htmlBody == null)
    {
        htmlBody = doc.CreateElement("body");
        //move all child of <html> to be child of <body>
        HtmlNodeCollection htmlCollection = htmlNode.ChildNodes;
        htmlBody.AppendChildren(htmlCollection);
        htmlNode.RemoveAllChildren();
        //add <body> to <html>
        htmlNode.PrependChild(htmlBody);
    }
    
    //check if <head> exists, if not create <head>
    HtmlNode head = doc.DocumentNode.SelectSingleNode("//head");
    if (head == null)
    {
        //add <head> to <html>
        head = doc.CreateElement("head");
        htmlNode.PrependChild(head);
    }
    .....
    

    【讨论】:

    • 非常好。小问题是
    猜你喜欢
    • 2021-01-20
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2020-10-23
    • 2020-07-26
    相关资源
    最近更新 更多