gpt4 book ai didi

c# - Html 敏捷包/C# : how to create/replace tags?

转载 作者:技术小花猫 更新时间:2023-10-29 12:51:07 25 4
gpt4 key购买 nike

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

使用 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/

25 4 0
文章推荐: xcode - 由于未跟踪的工作树文件导致 git branch checkout 出现问题
文章推荐: python - 用 Python 编写的 HTML 缩进器
文章推荐: python - 使用 Selenium 从网页中获取所有可见文本
文章推荐: android -