Grab all text from html with Html Agility Pack

后端 未结 6 809
执念已碎
执念已碎 2020-11-28 10:11

Input

foo bar baz

O

6条回答
  •  离开以前
    2020-11-28 10:12

    var root = doc.DocumentNode;
    var sb = new StringBuilder();
    foreach (var node in root.DescendantNodesAndSelf())
    {
        if (!node.HasChildNodes)
        {
            string text = node.InnerText;
            if (!string.IsNullOrEmpty(text))
                sb.AppendLine(text.Trim());
        }
    }
    

    This does what you need, but I am not sure if this is the best way. Maybe you should iterate through something other than DescendantNodesAndSelf for optimal performance.

提交回复
热议问题