Wrap in an element with HtmlAgilityPack?

c# html-agility-pack

Question

I have an HtmlDocument that may or may have a proper <head> and <body> section or might just be an html fragment. Either way, I want to run it through a function that will ensure that it has (more) proper html structure.

I know that I can check if it has a body by seeing if

doc.DocumentNode.SelectSingleNode("//body");

is null. If it does not have a body, how would I wrap the contents of doc.DocumentNode in a <body> element and assign it back to the HtmlDocument?

Edit: There seems to be some confusion about what I want to do. In jquery terms:

$doc = $(document);
if( !$doc.has('body') ) {
    $doc.wrapInner('body');
}

Basically, if there is no body element, put a body element around everything.

Accepted Answer

You could do something like this:

HtmlDocument doc = new HtmlDocument();
doc.Load(MyTestHtm);
HtmlNode body = doc.DocumentNode.SelectSingleNode("//body");
if (body == null)
{
    HtmlNode html = doc.DocumentNode.SelectSingleNode("//html");
    // we presume html exists

    body = CloneAsParentNode(html.ChildNodes, "body");
}


static HtmlNode CloneAsParentNode(HtmlNodeCollection nodes, string name)
{
    List<HtmlNode> clones = new List<HtmlNode>(nodes);
    HtmlNode parent = nodes[0].ParentNode;

    // create a new parent with the given name
    HtmlNode newParent = nodes[0].OwnerDocument.CreateElement(name);

    // insert before the first node in the selection
    parent.InsertBefore(newParent, nodes[0]);

    // clone all sub nodes
    foreach (HtmlNode node in clones)
    {
        HtmlNode clone = node.CloneNode(true);
        newParent.AppendChild(clone);
    }

    // remove all sub nodes
    foreach (HtmlNode node in clones)
    {
        parent.RemoveChild(node);
    }
    return newParent;
}



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