问题
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.
回答1:
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;
}
来源:https://stackoverflow.com/questions/16156217/wrap-in-an-element-with-htmlagilitypack