2010-05-10 105 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