代码之家  ›  专栏  ›  技术社区  ›  Rick

使用HTMLAgilityPack以特定顺序插入节点时遇到问题

  •  0
  • Rick  · 技术社区  · 10 年前

    我再次迷失在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);
    }
    

    这段代码给了我这个——你可以看到 <body> 在错误的地方。

    <html>
       <body>
          <head>
          ...
          </head>
          <table>
          ...
          </table>
      </body>
    </html>
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   har07    10 年前

    您可以尝试添加 <body> 添加之前先添加节点 <head> ,因为您似乎想要 <html> 除了 <头部> 放置在 <身体> 标签:

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