首页 > 解决方案 > 更改 HTML 属性的值

问题描述

如何更改 HtmlDocument 的属性值?

我尝试了什么:

destinationFileDocument.DocumentNode.SelectSingleNode(pathWithoutAttribute).Attributes[attributeName].Value = newValue;

没用。

string OldValue = destinationFileDocument.DocumentNode.SelectSingleNode(pathWithoutAttribute).Attributes[attributeName].Value;
destinationFileDocument.DocumentNode.SelectSingleNode(pathWithoutAttribute).SetAttributeValue(attributeName, NewValue);

也没有用。我实际上认为我的第一种方法应该有效。

如何更改值?

/edit:我的第二种方法是错误的,SetAttribute 中的第一个参数应该是属性名称。会试试这个 rn。/edit:仍然无法正常工作/edit:https://html-agility-pack.net/set-attribute-value setAttributeValue 如果已经存在值,似乎无法更改值。

/编辑:

string result = null;
using (StringWriter writer = new StringWriter())
{
    destinationFileDocument.Save(writer);
    result = writer.ToString();
}
HtmlDocument newDoc = new HtmlDocument();
newDoc.LoadHtml(result);
destinationFileDocument = newDoc;

将此添加到第二个解决方案中修复了它。

标签: c#html-agility-pack

解决方案


推荐阅读