【发布时间】:2020-09-24 20:40:43
【问题描述】:
我正在尝试更新与特定 xpath 匹配的多个节点的内容。例如,在下面的示例中,我想用“Hello”更新当前为“2”的任何 attribute1 节点。最好的方法是什么?
using System;
using System.IO;
using System.Xml;
using System.Linq;
public class Program
{
public static void Main()
{
XmlDocument job = new XmlDocument();
job.LoadXml(@"<parent>" +
"<report>" +
"<main>" +
"<attribute1>2</attribute1>"+
"<attribute1>2</attribute1>"+
"<attribute1>3</attribute1>"+
"</main>"+
"</report>" +
"</parent>");
string newAttribute1Value = "Hello";
//How do I update both attribute1's where value=2?
}
【问题讨论】: