HTML Agility Pack RemoveChild - 行为不符合预期
假设我想从此 html 中删除 span 标签:
<html><span>we do like <b>bold</b> stuff</span></html>
我希望这段代码能够完成我想要的操作,
string html = "<html><span>we do like <b>bold</b> stuff</span></html>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNode span = doc.DocumentNode.Descendants("span").First();
span.ParentNode.RemoveChild(span, true); //second parameter is 'keepGrandChildren'
但输出如下所示:
<html> stuff<b>bold</b>we do like </html>
它似乎正在反转跨度内的子节点。我做错了什么吗?
Say I want to remove the span tag from this html:
<html><span>we do like <b>bold</b> stuff</span></html>
I'm expecting this chunk of code to do what I'm after
string html = "<html><span>we do like <b>bold</b> stuff</span></html>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
HtmlNode span = doc.DocumentNode.Descendants("span").First();
span.ParentNode.RemoveChild(span, true); //second parameter is 'keepGrandChildren'
But the output looks like this:
<html> stuff<b>bold</b>we do like </html>
It appears to be reversing the child nodes within the span. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅供记录,这是我的版本,基于此问题的答案:
Just for the records, this is my version, based on the answers of this question:
看起来像 HtmlAgilityPack 中的错误 - 请参阅他们的问题寄存器:
http://htmlagilitypack.codeplex.com/workitem/9113
有趣的是,这是 4 年前提出的...
这是一个片段,它将删除所有 span 标签(或您指定的任何其他标签)并保持其他节点的正确顺序。
Looks like a bug in HtmlAgilityPack - see their issue register:
http://htmlagilitypack.codeplex.com/workitem/9113
Interestingly this was raised 4 years ago...
Here's a snippet that will remove all span tags (or any other tag you specify) and keeps other nodes in the correct order.