Html Agility Pack / C#:如何创建/替换标签?

任务很简单,但我找不到答案。

使用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; } 

如果它是只读的,你可以发布一些代码吗?