任务很简单,但我找不到答案。
使用 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;
}
关于c# - Html 敏捷包/C# : how to create/replace tags?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6540154/