问题描述
我正在尝试从一段HTML中删除img和map元素.
I'm trying to remove the img and map element from a piece of html.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var oldHtml = doc.DocumentNode.InnerHtml;
if (doc.DocumentNode.SelectNodes("//img[@usemap]") != null)
{
HtmlNode img = doc.DocumentNode.SelectSingleNode("//img[@usemap]");
img.ParentNode.RemoveChild(img);
}
if (doc.DocumentNode.SelectNodes("//map") != null)
{
HtmlNode map = doc.DocumentNode.SelectSingleNode("//map");
map.ParentNode.RemoveChild(map);
}
var newHtml = doc.DocumentNode.InnerHtml;
newHtml仍然包含img和map元素.在html更新之前,我还需要做其他事情吗?
The newHtml still contains the img and map element. Do I need to do something else before the html is updated?
这是我要剥离的html:
Here is the html that I'm trying to strip:
<p><img src="/media/8301/HD00_498x299.jpg" width="498" height="299" alt="HD00.JPG" usemap="#imgmap201392714219"/><br />
<br />
<a title="Download ZIP DWG"
href="/media/8103/detailtekeningen-dwg-unidek-aero.zip"
target="_blank">Klik hier om alle DWG bestanden in
een zipfile te downloaden.</a><br />
<a title="Download DXF"
href="/media/8104/detailtekeningen-dxf-unidek-aero.zip"
target="_blank">Klik hier om alle DXF bestanden in een zipfile te
downloaden.</a><br />
<a title="Download PDF"
href="/media/8116/detailtekeningen-pdf-unidek-aero.zip"
target="_blank">Klik hier om alle PDF bestanden in een zipfile te
downloaden.</a><br />
<br />
<strong><a title="Bouwdetails berekende psi-waarden"
href="/{localLink:8014}" target="_blank">Link naar de technische
bouwdetails met verbeterde eigen ψ-waarden<br />
</a></strong> <map name="imgmap2012104102243"
id="imgmap2012104102243">
<area title="" href="/nl/producten/hellend-dak/unidek-aero/1"
shape="rect" coords="194,419,219,439" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/2"
shape="rect" coords="221,420,246,439" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/3"
shape="rect" coords="200,302,226,320" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/4"
shape="rect" coords="209,167,234,185" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/6"
shape="rect" coords="68,46,98,67" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/7"
shape="rect" coords="102,203,129,224" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/8"
shape="rect" coords="273,339,302,360" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/9"
shape="rect" coords="387,350,417,372" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/10"
shape="rect" coords="324,341,354,363" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/11"
shape="rect" coords="223,369,252,390" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/12"
shape="rect" coords="62,270,89,294" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/13"
shape="rect" coords="93,270,119,294" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/14"
shape="rect" coords="31,94,60,114" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/14"
shape="rect" coords="79,161,106,182" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/15"
shape="rect" coords="19,150,50,171" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/15"
shape="rect" coords="82,113,110,134" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/16"
shape="rect" coords="176,231,205,253" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/17"
shape="rect" coords="147,179,176,200" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/18"
shape="rect" coords="139,235,166,257" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/19"
shape="rect" coords="204,56,231,78" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/20"
shape="rect" coords="125,135,153,157" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/21"
shape="rect" coords="265,263,290,284" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/23"
shape="rect" coords="9,202,36,225" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/24"
shape="rect" coords="39,202,65,225" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/25"
shape="rect" coords="158,80,184,101" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/26"
shape="rect" coords="188,80,213,102" target="_blank" alt="" />
</map><map id="imgmap201392714219">
<area title="" href="/nl/producten/hellend-dak/unidek-aero/1"
shape="rect" coords="265,463,279,480" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/2"
shape="rect" coords="282,466,297,480" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/3"
shape="rect" coords="213,339,237,358" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/4"
shape="rect" coords="206,204,227,220" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/6"
shape="rect" coords="113,105,135,121" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/7"
shape="rect" coords="134,246,154,262" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/8"
shape="rect" coords="299,369,319,386" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/9"
shape="rect" coords="432,409,453,425" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/10"
shape="rect" coords="363,394,385,413" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/11"
shape="rect" coords="254,406,276,422" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/12"
shape="rect" coords="105,298,122,314" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/13"
shape="rect" coords="122,298,139,314" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/14"
shape="rect" coords="53,121,77,139" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/15"
shape="rect" coords="49,165,72,182" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/16"
shape="rect" coords="195,272,214,288" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/17"
shape="rect" coords="152,212,175,230" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/18"
shape="rect" coords="160,276,180,293" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/19"
shape="rect" coords="234,88,255,105" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/20"
shape="rect" coords="132,155,158,174" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/21"
shape="rect" coords="299,294,321,311" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/23"
shape="rect" coords="40,234,55,250" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/24"
shape="rect" coords="56,233,73,251" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/25"
shape="rect" coords="185,108,202,127" target="_blank" alt="" />
<area title="" href="/nl/producten/hellend-dak/unidek-aero/26"
shape="rect" coords="203,109,219,127" target="_blank" alt="" />
</map></p>
当我调试img和map元素时,但是调用RemoveChild根本不会更改html.另外,当我尝试更改属性或其他任何东西时,都不会发生.
When I debug the img and map element are found, but calling RemoveChild doesn't change the html at all. Also when I try to change an attribute or something else nothing happens.
推荐答案
我刚刚发现HTML Agility Pack的错误在于,您只能要求一次.InnerHtml
.在那之后,它将不会更新.您要求两次:
I've just discovered that the bug with HTML Agility pack is that you can only ask for .InnerHtml
once. After that, it will not update. You are asking for it twice:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var oldHtml = doc.DocumentNode.InnerHtml;
if (doc.DocumentNode.SelectNodes("//img[@usemap]") != null)
{
HtmlNode img = doc.DocumentNode.SelectSingleNode("//img[@usemap]");
img.ParentNode.RemoveChild(img);
}
if (doc.DocumentNode.SelectNodes("//map") != null)
{
HtmlNode map = doc.DocumentNode.SelectSingleNode("//map");
map.ParentNode.RemoveChild(map);
}
var newHtml = doc.DocumentNode.InnerHtml;
如果您摆脱了这一行:
If you get rid of this line:
var oldHtml = doc.DocumentNode.InnerHtml;
应该可以. HtmlAgilityPack似乎是一个随机错误.
It should work. It seems to be a random bug with HtmlAgilityPack.
Sniffdk的解决方案之所以有效,是因为他只得到一次.OuterHtml
. HtmlUtilityPack家伙需要修复它.
Sniffdk's solution works because he only gets .OuterHtml
once. The HtmlUtilityPack guys need to fix that.
这篇关于HTML不会随HTML Agility Pack一起更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!