Linq to XML选择所有父项和子项

时间:2015-11-13 19:24:44

标签: c# asp.net xml linq

我正在尝试将Linq to XML查询的结果绑定到DataList控件,并且我无法返回所有子值。这是我的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>

1 个答案:

答案 0 :(得分: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();