4

给定以下 XML:

<SomeXML>
    <Element1>
        <Element2 Attribute3="Value4" />
    </Element1
</SomeXML>

...以及对 'SomeElement' 的 XElement 引用和 XPath 'Element1/Element2/@Attribute3'

如何检索对 Attribute3 的引用以便我可以更改它的值(使用 Xpath)?

XPath 将是一个检索设置,因此是我找到相关节点的唯一方法。

4

2 回答 2

6

添加using System.Xml.XPath到需要执行此操作的代码文件中。

然后你可以使用这样的代码: -

 var attrib3 = someElement.XPathEvaluate("Element1/Element2/@Attribute3") as XAttribute;
 if (attrib3 != null)
     attrib3.Value = "new value";
于 2010-05-18T14:03:24.150 回答
4

使用System.Xml.XPath

以及XElement 上的扩展方法XPathSelectElement

于 2010-05-18T14:02:27.537 回答