【问题标题】:Linq to XML Select All Parent and Child ItemsLinq to XML 选择所有父项和子项
【发布时间】:2016-02-15 11:34:25
【问题描述】:

我正在尝试将 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

看起来我已经很接近得到我想要的东西了,但我无法将它们全部结合在一起。任何帮助表示赞赏!

编辑

这是我的数据列表...

<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>

【问题讨论】:

    标签: c# asp.net xml linq


    【解决方案1】:

    我会写这样的东西

    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();
    

    【讨论】:

    • 这让我更接近了,但现在我的输出看起来像这样......
    • @Padraig75 喜欢什么?
    • 对不起...对 SO 来说还很陌生,并且在评论中遇到了困难。基本上我得到了父 1、子 1、父 1、子 2、父 1、子 3 等的垂直列表。而不是父 1、子 1、子 2、子 3
    • @Padraig75 我用你的 xml 测试了这段代码(还修复了缺少的 &lt;/categories&gt; 标签),它返回了有效的 6 项...
    • @Padraig75 它返回正确的allCats,所以答案是正确的。您的 new 问题不在您原来的问题中。你所做的不是提问的好方法。问一个好的/完整的并得到答案。用新请求扩展它不是 SO 的工作方式.....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多