2011-10-20 17 views
0

選擇的XElement元素及其子我需要在一個單一的步驟,選擇父母和孩子,這樣我可以讓他們爲IEnumerable和遍歷他們如何在一個單一的LINQ查詢

我試過parent.descendantsandself但不工作根據您所提供的,我認爲應該DescendantsAndSelf工作信息的任何想法

<MenuItem id="MyAccount" rmId="MyAccount" loc-attr="MyAccount,text" text="My Account" target="" display="true"> 
    <MenuItem id="MyProducts" rmId="MyProducts" loc-attr="MyProducts,text" text="My McAfee Products" target="" display="true" href="http://home.mcafee.com/root/myAccount.aspx" /> 
    <MenuItem id="MyProfile" rmId="MyProfile" loc-attr="MyProfile,text" text="Update McAfee Account Info" target="" display="true" href="https://home.mcafee.com/Secure/Protected/MyAccountInfo.aspx" /> 
    <MenuItem id="MyArSettings" rmId="MyArSettings" loc-attr="MyArSettings,text" text="My Auto-Renewal Settings" href="https://home.mcafee.com/Secure/Protected/AutoRenew.aspx" target="" display="true" /> 
</MenuItem> 

回答

0

,例如,當我嘗試

  XElement item = XElement.Parse(@"<MenuItem Id=""m1""> 
    <MenuItem Id=""m2""></MenuItem> 
    <MenuItem Id=""m3""></MenuItem> 
</MenuItem>"); 
      foreach (XElement menu in item.DescendantsAndSelf()) 
      { 
       Console.WriteLine("{0}", menu.Attribute("Id").Value); 
      } 

它妮採利輸出所有的三個元素的ID:

m1 
m2 
m3 

如果你仍然有代碼沒有做你想要它做的事情,然後考慮向我們展示了足夠的代碼,讓我們重現該問題有什麼問題。