这个任务很简单,但我找不到答案。
使用Node.Remove()方法很容易删除标签(节点)…但如何替换它们?
有一个ReplaceChild()方法,但它需要创建一个新标签。如何设置标签的内容?InnerHtml和OuterHtml是只读属性。
看一下这段代码:
public string ReplaceTextBoxByLabel(string htmlContent)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
foreach(HtmlNode tb in doc.DocumentNode.SelectNodes("//input[@type='text']"))
{
string value = tb.Attributes.Contains("value") ? tb.Attributes["value"].Value : " ";
HtmlNode lbl = doc.CreateElement("span");
lbl.InnerHtml = value;
tb.ParentNode.ReplaceChild(lbl, tb);
}
return doc.DocumentNode.OuterHtml;
}
你确定 InnerHtml 是只读属性吗?
HTMLAgility 包的文档说得不一样:(剪切并粘贴)
Gets or Sets the HTML between the start and end tags of the object.
Namespace: HtmlAgilityPack
Assembly: HtmlAgilityPack (in HtmlAgilityPack.dll) Version: 1.4.0.0 (1.4.0.0)
Syntax
C#
public virtual string InnerHtml { get; set; }
如果它是只读的,你能发布一些代码吗?