2015-11-13 65 views
-1

我試圖將Linq的結果綁定到XML查詢到DataList控件,並且無法返回所有子值。這裏是我的XML ...Linq to XML選擇所有父項和子項目

<categories> 
    <category> 
    <parent-id>1234</parent-id> 
    <parent-name>Parent 1</parent-name> 
    <children> 
     <child-cat> 
     <child-id>5678</child-id> 
     <child-name>Child 1</child-name> 
     </child-cat> 
     <child-cat> 
     <child-id>2824</child-id> 
     <child-name>Child 2</child-name> 
     </child-cat> 
     <child-cat> 
     <child-id>2831</child-id> 
     <child-name>Child 3</child-name> 
     </child-cat> 
    </children> 
    </category> 
    <category> 
    <parent-id>6398</parent-id> 
    <parent-name>Parent 2</parent-name> 
    <children> 
     <child-cat> 
     <child-id>5564</child-id> 
     <child-name>Child 1</child-name> 
     </child-cat> 
     <child-cat> 
     <child-id>2824</child-id> 
     <child-name>Child 2</child-name> 
     </child-cat> 
     <child-cat> 
     <child-id>2831</child-id> 
     <child-name>Child 3</child-name> 
     </child-cat> 
    </children> 
    </category> 

這裏是我的Linq查詢...

var categories = XDocument.Load(Server.MapPath("/app_data/ShoppingCategories.xml")); 
     var allCats = from category in categories.Root.Descendants("category") 
      select new 
         { 
          parentId = category.Descendants("parent-id").First().Value, 
          parentName = category.Descendants("parent-name").First().Value, 
          childId = category.Descendants("child-cat").First().Value, 
          childName = category.Descendants("child-name").First().Value 
         }; 
     dtlstCategories.DataSource = allCats; 
     dtlstCategories.DataBind(); 

我看起來就像這樣(我只顯示父名和兒童名稱字段,該ID字段被綁定到隱藏字段)...

父1      父2
兒童1              孩子2

因爲我使用.First()的子元素,我意識到這就是爲什麼只有第一個顯示,但我似乎無法弄清楚如何讓他們都顯示。這是我後...

父1      父2
兒童1            兒童1
兒童2            兒童2
兒童3            孩子3

看起來我很接近得到我想要的東西,但我無法將它們全部結合在一起。任何幫助表示讚賞!

編輯

這裏是我的DataList ...

<asp:DataList ID="dtlstCategories" runat="server" OnItemDataBound="dtlstCategories_ItemDataBound" Visible="True" RepeatColumns="5" RepeatDirection="Vertical" RepeatLayout="Table" ItemStyle-Wrap="True" BorderWidth="0" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" ItemStyle-Width="170"> 
     <ItemTemplate> 
      <div style="padding-left:10px; padding-right:10px;">  
       <asp:HiddenField ID="hdnTopCategoryId" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "parentId") %>' /> 
       <asp:LinkButton ID="lnkTopCategory" runat="server" CssClass="support" Text='<%#DataBinder.Eval(Container.DataItem, "parentName") %>'></asp:LinkButton> 
       <asp:Image ID="imgCatDivider" runat="server" /> 
       <asp:HiddenField ID="hdnChildCatId" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "childId") %>' /> 
       <asp:LinkButton ID="lnkChildName" runat="server" CssClass="support" Text='<%#DataBinder.Eval(Container.DataItem, "childName") %>'></asp:LinkButton> 
      </div> 
     </ItemTemplate> 
    </asp:DataList> 

回答

2

我會寫這樣的事情

var allCats = categories.Descendants("child-cat") 
    .Select(c => new 
    { 
     parentId = c.Parent.Parent.Element("parent-id").Value, 
     parentName = c.Parent.Parent.Element("parent-name").Value, 
     childId = c.Element("child-id").Value, 
     childName = c.Element("child-name").Value 
    }) 
    .ToList(); 
+0

這讓我更接近,但現在我的輸出看起來像這樣...
Padraig75

+0

@ Padraig75像什麼? – Eser

+0

對不起......對於SO來說很新,並且在評論中掙扎着。基本上,我得到了父母1,孩子1,父母1,孩子2,父母1,孩子3等的垂直列表。而不是父母1,孩子1,孩子2,孩子3 – Padraig75