我正在尝试用h2标签替换所有h1标签,而我正在使用
HtmlAgility包.
我这样做了:
var headers = doc.DocumentNode.SelectNodes("//h1"); if (headers != null) { foreach (HtmlNode item in headers) { //item.Replace?? } }
我被卡在那里我试过这个:
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); } }
解决方法
var headers = doc.DocumentNode.SelectNodes("//h1"); if (headers != null) { foreach (HtmlNode item in headers) { item.Name = "h2" } }