将节点与Html Agility Pack结合起来的最佳方法

将节点与Html Agility Pack结合起来的最佳方法

问题描述:

我已经将大文档从Word转换为HTML。这很接近,但我有一堆“代码”节点,我想合并成一个“前”节点。将节点与Html Agility Pack结合起来的最佳方法

这里的输入:

<p>Here's a sample MVC Controller action:</p> 
<code>  public ActionResult Index()</code> 
<code>  {</code> 
<code>   return View();</code> 
<code>  }</code> 
<p>We'll start by making the following changes...</p> 

我希望把它变成这样,而是:

<p>Here's a sample MVC Controller action:</p> 
<pre class="brush: csharp">  public ActionResult Index() 
    { 
     return View(); 
    }</pre> 
<p>We'll start by making the following changes...</p> 

我最后写一个暴力循环,寻找个连续的迭代节点,但这看起来对我来说很难看:

HtmlDocument doc = new HtmlDocument(); 
doc.Load(file); 

var nodes = doc.DocumentNode.ChildNodes; 
string contents = string.Empty; 

foreach (HtmlNode node in nodes) 
{ 

    if (node.Name == "code") 
    { 
     contents += node.InnerText + Environment.NewLine; 
     if (node.NextSibling.Name != "code" && 
      !(node.NextSibling.Name == "#text" && node.NextSibling.NextSibling.Name == "code") 
      ) 
     { 
      node.Name = "pre"; 
      node.Attributes.RemoveAll(); 
      node.SetAttributeValue("class", "brush: csharp"); 
      node.InnerHtml = contents; 
      contents = string.Empty; 
     } 
    } 
} 

nodes = doc.DocumentNode.SelectNodes(@"//code"); 
foreach (var node in nodes) 
{ 
    node.Remove(); 
} 

通常我会删除第一个循环中的节点,但不会w在迭代过程中,因为在迭代它时无法更改集合,

更好的主意?

清理您想要解析的html。 HTML Agility Pack strip tags NOT IN whitelist

第一种方法:选择所有的<code>节点,它们分组,并创建每组一个<pre>节点:

var idx = 0; 
var nodes = doc.DocumentNode 
    .SelectNodes("//code") 
    .GroupBy(n => new { 
     Parent = n.ParentNode, 
     Index = n.NextSiblingIsCode() ? idx : idx++ 
    }); 

foreach (var group in nodes) 
{ 
    var pre = HtmlNode.CreateNode("<pre class='brush: csharp'></pre>"); 
    pre.AppendChild(doc.CreateTextNode(
     string.Join(Environment.NewLine, group.Select(g => g.InnerText)) 
    )); 
    group.Key.Parent.InsertBefore(pre, group.First()); 

    foreach (var code in group) 
     code.Remove(); 
} 

这里的分组字段是父节点的组合的字段和组索引被增加当找到新组时。 此外,我使用NextSiblingIsCode扩展方法这里:

public static bool NextSiblingIsCode(this HtmlNode node) 
{ 
    return (node.NextSibling != null && node.NextSibling.Name == "code") || 
     (node.NextSibling is HtmlTextNode && 
     node.NextSibling.NextSibling != null && 
     node.NextSibling.NextSibling.Name == "code"); 
} 

它用来确定下一个同级是否是<code>节点。


第二种方法:只选择每个组的顶部 <code>节点,然后遍历每个节点,找到下一个节点,直到第一个节点为非节点。我在这里使用了 xpath

var nodes = doc.DocumentNode.SelectNodes(
    "//code[name(preceding-sibling::*[1])!='code']" 
); 
foreach (var node in nodes) 
{ 
    var pre = HtmlNode.CreateNode("<pre class='brush: csharp'></pre>"); 
    node.ParentNode.InsertBefore(pre, node); 
    var content = string.Empty; 
    var next = node; 
    do 
    { 
     content += next.InnerText + Environment.NewLine; 
     var previous = next; 
     next = next.SelectSingleNode("following-sibling::*[1][name()='code']"); 
     previous.Remove(); 
    } while (next != null); 
    pre.AppendChild(doc.CreateTextNode(
     content.TrimEnd(Environment.NewLine.ToCharArray()) 
    )); 
}