c# - Best way to combine nodes with Html Agility Pack -
I've changed a large document from Word to HTML, it's close, but I have a bunch of "code" nodes Which I want to merge into a "pre" node.
Here is the input:
& lt; P & gt; Here is a sample MVC controller action: & lt; / P & gt; & Lt; Code & gt; Public Performance Index () & lt; / Code & gt; & Lt; Code & gt; {& Lt; / Code & gt; & Lt; Code & gt; See Return (); & Lt; / Code & gt; & Lt; Code & gt; } & Lt; / Code & gt; & Lt; P & gt; We will start by making the following changes ... & lt; / P & gt;
I want to change it in this, instead:
& lt; P & gt; Here is a sample MVC controller action: & lt; / P & gt; & Lt; Pre-class = "brush: CSRAP" & gt; Public Performance Index () {Return View (); } & Lt; / Pre & gt; & Lt; P & gt; We will start by making the following changes ... & lt; / P & gt;
I have finished writing a loop-force loop that constantly leads nodes in search of people, but it looks ugly to me:
< Code> HtmlDocument doc = new HtmlDocument (); Doc.Load (file); Var nodes = doc.DocumentNode.ChildNodes; String content = string Empty; Foreach (HtmlNode node in nodes) {if (node.Name == "code") {contents + = node.InnerText+ Environment.NewLine; If (node.NextSibling.Name! = "Code" & amp; (Node.NextSibling.Name == "#text" and Node. Next.Sibling.NextSibling.Name == "Code")) {node. Name = "ex"; Node.Attributes.RemoveAll (); Node .set attribute value ("class", "brush: seashore"); Node.InnerHtml = Content; Content = string Empty; }}} Nodes = doc.DocumentNode.SelectNodes (@ "// code"); Forresh (node in nodes) {node. Revove (); }
Usually I will remove the nodes in the first loop, but it does not work during the recurrence because you can not change that archive because you iterate it.
Better thoughts?
The first method: all & lt; Code & gt; / Code> nodes, group them together, and a
and lt; Pre & gt;
Create a node per group:
var idx = 0; Var node = doc.DocumentNode .SelectNodes ("// code") .GroupBy (n = & gt; new {parent = n.ParentNode, index = n.NextSiblingIsCode ()? Idx: idx ++}); Vorach (Ver group in nodes) {var pre = HtmlNode.CreateNode ("pre-class = 'brush: csharp'> gt; & lt; / pre & gt;); Pre.AppendChild (doc.CreateTextNode (string.Join (EnvironmentalNewLine, group.Select (g = & gt; g.InteText))); group. K.Internet (East, Group.First ()); Foreign (different codes in the group) code. Remove (); }
The group field here is a parent node and the combined field of the group index, which increases when found in new groups. Apart from this, I used the Next SiblingIscode
extension method here:
public static boole next symbolizing iSCode (this HTML node) {return (node.exbling! = Null and node Next silting .name == "code") || || (Node.NextSibling HtmlTextNode & Node. NextSibling.NextSibling! = Faucet & Node. NextSibling.NextSibling.Name == "Code"); }
It is determined that the next brother has a & lt; Code & gt;
node is
second approach: only the top
of each group node, then enter each code & gt;
before the non- & lt; Code & gt;
Node I used here xpath var nodes = doc.DocumentNode.SelectNodes ("// code [name (preceding-sibling :: * [1])! = 'Code'] "); Forward (in nodes in nodes) {var pre = HtmlNode.CreateNode ("prefix = 'brush: csharp'> gt; & lt; / pre & gt;); Node.ParentNode.InsertBefore (formerly, node); Var content = string.Empty; Var next = node; What {content + = next.InteText+ Environment.NewLine; Var previous = next; Next = next Select Sense Node ("Following-Siblings :: * [1] [Name () = 'Code']"); Previous.Remove (); } While (next! = Null); Pre.AppendChild (doc.CreateTextNode (content.TrimEnd (EnvironmentalNewLine.ToCharArray ())); }
Comments
Post a Comment