2013-12-13 75 views
0

如果myVariable = "Element"然後給我那個元素中的屬性。來自特定元素節點的XML抓取屬性

XML:

<Products> 
<Equity> 
    <servers> 
     <serverEQ>server1</serverEQ> 
     <serverEQ>server2</serverEQ> 
     <serverEQ>server3</serverEQ> 
    </servers> 
<sitesE> 
     <sitesEQ sitePathEQ="\Logs\W3SVC1"><nameEQ>SystemAdmin Site</nameEQ></sitesEQ> 
     <sitesEQ sitePathEQ="\Logs\W3SVC3"><nameEQ>Direct Access Site</nameEQ></sitesEQ> 
     <sitesEQ sitePathEQ="\Logs\W3SVC4"><nameEQ>Redirect Site</nameEQ></sitesEQ> 
     <sitesEQ sitePathEQ="\Logs\W3SVC5"><nameEQ>Download Site</nameEQ></sitesEQ> 
</sitesE> 
</Equity> 
. 
. 
. 
</Products> 

例如:如果myVariable = "Direct Access Site"給我\Logs\W3SVC3作爲我的輸出。

+0

我發現侯賽因Narimani Rad公司的回答最有用的。謝謝 – SlopTonio

回答

3

不清楚你想要什麼,但是這可能幫助:

var myVariable = "Direct Access Site"; 
XDocument doc = XDocument.Load(your file); 
var result = doc.Descendants("sitesEQ") 
       .Where(i => i.Element("nameEQ").Value == myVariable) 
       .Select(i => i.Attribute("sitePathEQ").Value); 

foreach (string item in result) 
{ 
    Console.WriteLine(item); 
} 
+0

非常感謝! – SlopTonio