简体   繁体   English

如何找到属性值

[英]How to find attribute value

I need to find an inner text of an element inside an XmlDocument and return it's Xpath. 我需要在XmlDocument中找到元素的内部文本,然后返回它的Xpath。 for example, searching for "ThisText" inside : 例如,在内部搜索“ ThisText”:

<xml> 
<xml2 val="ThisText"></xml2> 
</xml> 

should return the Xpath of xml2 应该返回xml2的Xpath

what's the most efficient way of doing this in c#? 在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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM