Having problems with inserting node in a specific order using HTMLAgilityPack

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

Question

I am once again lost with HTMLAgility.

Here is the HTML string I am working with:

<table>...</table>

I am trying to correct this by adding:

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

Here is my code, I am able to get everything except the body. Any advise?

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

This code is giving me this -- as you can see the <body> is in the wrong place.

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

Accepted Answer

You can try to add <body> node first before adding <head>, because it seems that you want all content of <html> except <head> to be placed within <body> tag :

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



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why