开发者

How to find attribute value

开发者 https://www.devze.com 2022-12-29 14:02 出处:网络
I need to find an inner text of an element inside an XmlDocument and return it\'s Xpath. for example, searching for \"ThisText\" inside :

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号