开发者

How to find attribute value

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜