替换HtmlAgility中的标签 [英] Replacing tags in HtmlAgility
本文介绍了替换HtmlAgility中的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将所有h1
标签替换为h2
标签,并且正在使用HtmlAgility包.
I'm trying to replace all of my h1
tags with h2
tags and I'm using HtmlAgility pack.
我这样做了:
var headers = doc.DocumentNode.SelectNodes("//h1");
if (headers != null)
{
foreach (HtmlNode item in headers)
{
//item.Replace??
}
}
我被困在那里.我已经尝试过了:
and i got stuck there. I've tried this:
var headers = doc.DocumentNode.SelectNodes("//h1");
if (headers != null)
{
foreach (HtmlNode item in headers)
{
HtmlNode newNode = new HtmlNode(HtmlNodeType.Element, doc, item.StreamPosition);
newNode.InnerHtml = item.InnerHtml;
// newNode suppose to set to h2
item.ParentNode.ReplaceChild(newNode, item);
}
}
问题是,我不知道如何创建新的h2,获取所有属性等. 我确定有简单的方法可以做到这一点,有什么想法吗?
problem there is that i have no idea how to create a new h2, get all the attributes etc. i'm sure theres a simple way to do that, any ideas?
推荐答案
var headers = doc.DocumentNode.SelectNodes("//h1");
if (headers != null)
{
foreach (HtmlNode item in headers)
{
item.Name = "h2"
}
}
这篇关于替换HtmlAgility中的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文