Hello! Can you please tell me how to access the node with its value?

  • It's not entirely clear what you specifically want. What is the value of a node? What if there are many nodes with this value? Give an example of XML and what you need to find. - VladD
  • Why do you need access to the node, if you already have its value? - Andrey NOP

2 answers 2

Load your XML into an XDocument or XElement and use the Descendants() method without parameters — this will return a flat sequence of absolutely all the nodes of the original document, in which you will find suitable nodes, for example, using Where() :

 var doc = XDocument.Load(...); var elements = doc.Descendants().Where(e => e.Value == "...").ToList(); 

    You can get such nodes using an XPath query //*[text()='значение_узла'] .

    If you use XElement / XDocument to work with xml, then to use XPath, you need to connect the namespace System.Xml.XPath . The XPathSelectElement and XPathSelectElements extension methods will be available:

     var nodes = xml.XPathSelectElements("//*[text()='значение_узла']"); 

    For old XmlElement / XmlDocument, use the SelectSingleNode or SelectNodes :

     var nodes = xml.SelectNodes("//*[text()='значение_узла']") 
    • When you use XElement/XDocument , there is no reason at all to use a poor XPath , when you have everything in your hands to work through LINQ. - Bulson
    • @Bulson, well, the answer is to be as an alternative. The author of the question will suit one option, another user who has boosted this question is another. The more solutions, the better, I think so. - Andrey NOP