【问题标题】:Putting orphaned text into tags with HTMLAgilityPack使用 HTMLAgilityPack 将孤立文本放入标签中
【发布时间】:2014-08-29 02:31:49
【问题描述】:

我如何转换这样一段html的语法

<div>
     some text
     <br/>
     goes in here
     <br/>
     with only br tags
     <br/>
     to separate it
     <br/>
</div>

到这里

<div>
     <p>some text</p>
     <p>goes in here</p>
     <p>with only br tags</p>
     <p>to separate it</p>
</div>

在 c# 中使用 HTML 敏捷包?

【问题讨论】:

    标签: c# html html-agility-pack


    【解决方案1】:

    一种可能的方式:

    var html = @"<div>
         some text
         <br/>
         goes in here
         <br/>
         with only br tags
         <br/>
         to separate it
         <br/>
    </div>";
    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    var div = doc.DocumentNode.SelectSingleNode("div");
    //select all non-empty text nodes within <div>
    var texts = div.SelectNodes("./text()[normalize-space()]");
    foreach (var text in texts)
    {
        //remove current text node
        text.Remove();
        //replace with : <p>current text node content</p>
        var p = doc.CreateElement("p");
        p.AppendChild(doc.CreateTextNode(text.InnerText));
        div.PrependChild(p);
    }
    //remove all <br/> tags within <div>
    foreach (var selectNode in div.SelectNodes("./br"))
    {
        selectNode.Remove();
    }
    //print result
    Console.WriteLine(doc.DocumentNode.OuterHtml);
    

    【讨论】:

    • 看起来不错,但 text.Remove() 不应该在 foreach 块的末尾吗?当需要创建文本节点时,它将不再存在
    • 不一定。 text 变量在调用Remove() 后仍然包含相同的对象,只是它不再属于父&lt;div&gt; 元素..
    • 我的 HtmlAgilityPack 版本是用于 WinRT 的,它没有 SelectNodesSelectSingleNode。我尝试用Descendants() 和linq 查询替换它,但不支持xpath。知道如何在 linq 中执行 ./text()[normalize-space()] 吗?
    【解决方案2】:

    我采用了一种稍微不同的方法,将 div 的 innerHTML 视为文本,并使用&lt;br&gt; 对其进行拆分。这有点骇人听闻,但它确实有效。

    var html = @"<div>
         some text
         <br/>
         goes in here
         <br/>
         with only br tags
         <br/>
         to separate it
         <br/>
    </div>";
    
    var doc = new HtmlDocument();
    doc.LoadHtml(html);
    
    var divs = doc.DocumentNode.Descendants("div");
    //select all non-empty text nodes within <div>
    
    foreach (var div in divs)
    {
        // create a list of p nodes
        var ps = new List<HtmlNode>();
    
        // split text by "<br>"
        var texts = div.InnerHtml.Split(new string[]{ "<br>" }, StringSplitOptions.None);
    
        // iterate over split text
        foreach (var text in texts)
        {
            // if the line is not empty, add it to the collection
            if (!string.IsNullOrEmpty(text.Trim()))
            {
                var p = doc.CreateElement("p");
                p.AppendChild(doc.CreateTextNode(text));
                ps.Add(p);
            }
        }
    
        // join the p collection and paste it into the div
        div.InnerHtml = string.Join("", ps.Select(x => x.OuterHtml));
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 2014-02-23
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多