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

时间:2014-08-07 01:22:35

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

我再次迷失了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 个答案:

答案 0 :(得分:1)

您可以先尝试添加<body>节点,然后再添加<head>,因为您似乎希望除<html>之外的<head>的所有内容都放在<body>内标签:

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