c# - 更改 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;
将此添加到第二个解决方案中修复了它。
解决方案
推荐阅读
- javascript - How to cancel awaiting messages
- types - 在 Julia 中,为什么在两个元素元组上使用 Pair?
- php - Regex allow only datetime (h:m:s) or an integer
- android - 使用 Kotlin 检查 CheckBoxPreference 时如何启动函数?
- node.js - Download all linked mp3 files from a website using the terminal
- php - Laravel Nova:如何创建自定义字段及其 crud 方法?
- python - 函数绑定到 Tkinter 中的组合框
- python - 为什么 XPATH 的 find_element 在迭代日期选择器时会提供太多的 Web 元素?
- python - 实现“查找所有”算法,在表格中显示匹配的行,并在单击表格单元格时跳转到行
- python - 如何在python中调用输入的数据类型