2010-05-10 98 views
1

我需要在XmlDocument中查找元素的内部文本并返回它的Xpath。例如,搜索“ThisText”里面:如何查找属性值

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

应返回XML2

什么在C#这样做的最有效的方法中的XPath?

回答

1

试试这个:

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

谢谢延Granlund公司 – 2010-05-10 08:29:26

+0

@Pramodh,找你的欢迎。 – 2010-05-10 08:34:40