Html敏捷包/C#:如何创建/替换标签
本文关键字:创建 标签 替换 何创建 Html | 更新日期: 2023-09-27 18:01:01
任务很简单,但我找不到答案。
使用Node.Remove((删除标记(节点(很容易…但是如何替换它们呢?
有一个ReplaceChild((方法,但它需要创建一个新标记。如何设置标签的内容?内部Html和外部Html是只读属性。
请参阅以下代码片段:
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; }
如果它是只读的,你能发布一些代码吗?