Given XElement, how do I get a link to another relative XElement / Xattribute based on XPath?

Given the following XML:

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

... and the XElement reference to "SomeElement" and the Xath element of the Xath1 / Element2 / @ Attribute3 element

How to get a link to Attribute3 so that I can change its value (using Xpath)?

XPath must be a restored parameter, and thus is the only way to search for a node.

+4
source share
2 answers

Add using System.Xml.XPath to the code file where you need to do this.

Then you can use this code: -

  var attrib3 = someElement.XPathEvaluate("Element1/Element2/@Attribute3") as XAttribute; if (attrib3 != null) attrib3.Value = "new value"; 
+6
source

using System.Xml.XPath

and XPathSelectElement extension method on your XElement

+4
source

Source: https://habr.com/ru/post/1310118/


All Articles