static string OutputHtml = @"<a href=""https://twitter.com/RedGiantNews"" target=""_blank""><img
src=""http://image.e.redgiant.com/lib/998.png"" width=""24"" border=""0""
alt=""Twitter"" title=""Twitter"" class=""smImage""></a><a
href = ""https://www.facebook.com/RedGiantSoftware"" target=""_blank""><img
src = ""http://image.e.redgiant.com/lib/db5.png"" width=""24"" border=""0""
alt=""Facebook"" title=""Facebook"" class=""smImage""></a>
<a href = ""https://www.instagram.com/redgiantnews/"" target=""_blank""><img
src = ""http://image.e.redgiant.com/aa10-f8747e56f06d.png"" width=""24""
border=""0"" alt=""Instagram"" title=""Instagram"" class=""smImage""></a>";
我从原始的 HTML 代码中移除了浮动链接(
http://click.e.redgiant.com/?qs=d2ad061f)。
方法一:
public static string RemoveAllImageNodes(string html)
{
try
{
HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
var nodes = document.DocumentNode.SelectNodes("//img");
foreach (var node in nodes)
{
node.Remove();
}
html = document.DocumentNode.OuterHtml;
return html;
}
catch (Exception ex)
{
throw ex;
}
}
方法二:
public static string RemoveAllImageNodes(string html)
{
try
{
HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
if (document.DocumentNode.InnerHtml.Contains("<img"))
{
foreach (var eachNode in document.DocumentNode.SelectNodes("//img"))
{
eachNode.Remove();
}
}
html = document.DocumentNode.OuterHtml;
return html;
}
catch (Exception ex)
{
throw ex;
}
}
输出 HTML:
<a href="https://twitter.com/RedGiantNews" target="_blank"></a>
<a href="https://www.facebook.com/RedGiantSoftware" target="_blank"></a>
<a href="https://www.instagram.com/redgiantnews/" target="_blank"></a>
移除“img”标签中仅包含“src”属性后的HTML输出结果:
<a href="https://twitter.com/RedGiantNews" target="_blank"><img width="24" border="0" alt="Twitter" title="Twitter" class="smImage"></a>
<a href="https://www.facebook.com/RedGiantSoftware" target="_blank"><img width="24" border="0" alt="Facebook" title="Facebook" class="smImage"></a>
<a href="https://www.instagram.com/redgiantnews/" target="_blank"><img width="24" border="0" alt="Instagram" title="Instagram" class="smImage"></a>
<img>
节点中的“src”? - user8697090