views:

394

answers:

1

Hi all,

I am stuck on a problem. I think I am probably close to the solution but can not seem to get there. I have an XMLDoc that is loaded from a stored Proc, into a dataset and the xml is pulled from it, (don't think I can change any of this part). The XML looks like this:

<data>
  <datanode>
    <name>11</name>
    <group>G1</group>
    <value>val1</value>
  </datanode>
  <datanode>
    <name>12</name>
    <group>G1</group>
    <value>val2</value>
  </datanode>  
  <datanode>
<name>21</name>
<group>G2</group>
<value>val1</value>
  </datanode>
  <datanode>
<name>22</name>
<group>G2</group>
<value>val2</value>
  </datanode>
  <datanode>
<name>23</name>
<group>G2</group>
<value>val3</value>
  </datanode>  
  <datanode>
<name>31</name>
<group>G3</group>
<value>val1</value>
  </datanode>
  <datanode>
<name>32</name>
<group>G3</group>
<value>val2</value>
  </datanode>
  <datanode>
<name>33</name>
<group>G3</group>
<value>val3</value>
  </datanode>
  <datanode>
<name>34</name>
<group>G3</group>
<value>val4</value>
  </datanode>
</data>

I need to use nested repeaters to display this information. What I want is to group it by the "group" node then show each of the "datanodes" that belong to that "group" node. So it would look something like this on the page:

G1 - has 2 nodes
11 - Val1
12 - Val2

G2 - has 3 nodes
21 - Val1
22 - Val2
23 - Val3

etc

What I have so far: Default.aspx:

    <asp:Repeater ID="Repeater1" runat="server">

    <ItemTemplate>
        <p>                
            COUNT: <asp:Literal runat="server" ID="Literal3" Text='<%# XPath("count(group)") %>' />                
            <br />Text: <asp:Literal runat="server" ID="Literal4" Text='<%# XPath("text()") %>' />
            <br />Group: <asp:Literal runat="server" ID="Literal2" Text='<%# XPath("group") %>' />
            <br />ID: <asp:Literal runat="server" ID="displayname" Text='<%# XPath("name") %>' />
            <br />VAL: <asp:Literal runat="server" ID="Literal1" Text='<%# XPath("value") %>' />
         </p>            

    </ItemTemplate>

    </asp:Repeater>

Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    private XmlNodeList nodelist;

    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(@"C:\development\testforWork\WebApplication1\WebApplication1\data.xml");

        nodelist = doc.SelectNodes("//datanode[not(group=preceding-sibling::datanode/group)]/group");           
        //nodelist = doc.SelectNodes("//datanode[group='G2']");

        Repeater1.DataSource = nodelist;
        Repeater1.DataBind();
    }
}

I can get them to just display each datanode one after the other, but I can not figure out how to nest the repeaters and do the grouping. I am newish to XPath and repeaters and think I must have some gap in my knowledge and just can't connect the dots, (unless I am running down the wrong road entirely).

Thanks

A: 

OK so found my solution after some more experimenting:

<asp:Repeater ID="repManifestNumber" runat="server" DataSource="<%# ManifestDataTemp %>">
                    <ItemTemplate>
                            Manifest Number:
                            <%# CurrentManifestNumber = XPath("manifest_number").ToString() %>                                
                             - <%# XPath("count(..//box_item[manifest_number=" + CurrentManifestNumber + "])")%> Box(s)

                        <div class="showhide">
                                <asp:Repeater ID="repMainfestDetail" runat="server" DataSource='<%# ((XmlNode)Container.DataItem).SelectNodes("..//box_item[manifest_number=" + CurrentManifestNumber + "]") %>'>
                                    <HeaderTemplate>
                                        <table class="orders" id="manifest_<%# CurrentManifestNumber %>">
                                            <tr>
                                                <th>
                                                    Box Number
                                                </th>
                                                <th>
                                                    Date Dispatched
                                                </th>
                                                <th>
                                                    <input type="checkbox" class="checkboxflipflop_local" />
                                                    - Select All
                                                </th>
                                            </tr>
                                            <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <tr class="odd-table-row">
                                            <td>
                                                <asp:Literal runat="server" ID="litBoxNumber" Text='<%# XPath("box_number") %>' />
                                            </td>
                                            <td>
                                                <asp:Literal runat="server" ID="litErpDispatched" Text='<%# Convert.ToDateTime(XPath("date_dispatched")).ToString("dd/MM/yyyy") %>' />
                                            </td>
                                            <td>
                                                <asp:Literal runat="server" ID="litHiddenXmlNode" Text='<%# ((XmlNode)Container.DataItem).OuterXml %>'
                                                    Visible="false"></asp:Literal>
                                                <asp:CheckBox ID="chkIsReceived" runat="server" Checked="false" CssClass="storecheckbox" />
                                            </td>
                                        </tr>
                                    </ItemTemplate>

                                </asp:Repeater>                                
                        </table>
                        </div>
                    </ItemTemplate>

                </asp:Repeater>

Hope this might help someone else in the future (or me when I forget it again).

Jon