I need to find an inner text of an element inside an XmlDocument and return it's Xpath. for example, searching for "ThisText" inside :
<xml>
<xml2 val="ThisText"></xml2>
开发者_StackOverflow中文版</xml>
should return the Xpath of xml2
what's the most efficient way of doing this in c#?
Try this:
string xml = "<xml><xml2 val=\"ThisText\"/><xml2 val=\"ThatText\"/></xml>";
var doc = XDocument.Parse(xml);
var node = doc.Descendants().First(x => x.Attribute("val") != null
&& x.Attribute("val").Value == "ThisText");
Trace.WriteLine(node);
精彩评论