Hello! Can you please tell me how to access the node with its value?
2 answers
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 poorXPath, 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
|