2013-06-24 81 views
0

我有一個包含時間節點屬性值的下拉列表。 我想選擇的子子子節點根據父屬性的值屬性根據xml的子節點的某個屬性值使用linq子子節點屬性

XML是如下

<info> 
<time value="0-30"> 
    <id t_id="1" speaker="Rajesh " desc="welcome" /> 
    <id t_id="2" speaker="Deepak " desc="to the meeting" /> 
</time> 
<time value="31-50"> 
    <id t_id="1" speaker="Vishal" desc="welcome" /> 
    <id t_id="2" speaker="Vikas" desc="to the meeting" /> 
</time> 
</info> 

當我在下拉Rajesh和迪帕克選擇0-30必須顯示

我使用LINQ

請幫我

回答

0

選擇匹配時間元素(一個或多個)嘗試,然後壓平遞減endant ID元素

XDocument xdoc = XDocument.Load(path_to_xml); 
var speakers = xdoc.Descendants("time") 
        .Where(t => (string)t.Attribute("value") == "0-30") 
        .SelectMany(t => t.Descendants("id")) 
        .Select(id => (string)id.Attribute("speaker")); 

查詢語法

var speakers = from t in xdoc.Descendants("time") 
       where (string)t.Attribute("value") == "0-30" 
       from id in t.Descendants("id") 
       select (string)id.Attribute("speaker"); 

的XPath

var speakers = from id in xdoc.XPathSelectElements("//time[@value='0-30']/id") 
       select (string)id.Attribute("speaker"); 
+0

thanx您的回覆,我想存儲的名稱和說明在一些繩子,讓我要綁定到這些價值觀gridview –

+1

非常感謝。我明白了。再次感謝 –