C# 如何使用HTML Agility Pack和C删除HTML源代码中的空白#
在发布之前,我尝试了此线程的解决方案: 下面是我正在使用的HTML的一个片段:C# 如何使用HTML Agility Pack和C删除HTML源代码中的空白#,c#,html,html-agility-pack,C#,Html,Html Agility Pack,在发布之前,我尝试了此线程的解决方案: 下面是我正在使用的HTML的一个片段: <p>This is my text</p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </
<p>This is my text</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>This is next text</p>
上面代码的输出是
<p>This is my text</p>
<p>This is next text</p>
这是我的文本
这是下一篇课文
因此,我的问题是如何删除HTML源代码中两段之间的额外空白。删除第一段和最后一段之间的文本节点: HTML: 结果:
<p>This is my text</p>
<p>This is next text</p>
这是我的文本这是下一个文本
如果要保持段落之间的换行符,请使用for
循环,并在除最后一个文本节点之外的所有节点上调用Remove()
for (int i = 0; i < followingText.Count - 1; ++i)
{
followingText[i].Remove();
}
for(int i=0;i
结果:
<p>This is my text</p>
<p>This is next text</p>
这是我的文本
这是下一篇课文
请回答您的问题,以显示在您的foreach
循环后,您的代码如何输出结果。从foreach中使用的集合中删除节点通常会导致问题和异常-请检查您是否收到任何异常。祝您好运,它没有收到任何错误消息。谢谢。您正在删除整个项目,请参阅节点
。您应该做的是item.InnterHtml=string.Empty
。
for (int i = 0; i < followingText.Count - 1; ++i)
{
followingText[i].Remove();
}
<p>This is my text</p>
<p>This is next text</p>