none
XPath, XmlDataSource and selecting multiple elements of a node.

    Question

  • Hey all, I'm trying to nest DataLists in an ASP.NET page and display data from a single XML file.  A sample of my XML file is as follows:
    <Students>
     <Student id="1" birthday="04/20/1976">
      <Name>Jack</Name>
      <Pets>
       <Pet type="Dog">Missy</Pet>
       <Pet type="Dog">Monty</Pet>
       <Pet type="Cat">Felix</Pet>
      </Pets>
     </Student>
     <Student id="2" birthday="12/04/1995">
      <Name>Jill</Name>
      <Pets>
       <Pet type="Snake">Fido</Pet>
      </Pets>
     </Student>
     <Student id="3" birthday="05/04/1986">
      <Name>Jane</Name>
      <Pets>
       <Pet type="Cat">Fluffy</Pet>
       <Pet type="Cat">Sir. Winston of Canterbury, the Third</Pet>
       <Pet type="Cat">Envy</Pet>
       <Pet type="Rabbit">Snuggles</Pet>
      </Pets>
     </Student>
     <Student id="4" birthday="03/12/1985">
      <Name>John</Name>
      <Pets>
       <Pet type="Dog">Daisy</Pet>
       <Pet type="Dog">Duke</Pet>
      </Pets>
     </Student>
    </Students>
    

    In my XML Data Source, I call the single XML file without specifying an .XSL or XPath Expression.  Then, in my DataList (which references the XMLDataSource), I have the following markup:

      <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Students.xml">
      </asp:XmlDataSource>
      <asp:DataList ID="students" runat="server" DataSourceID="XmlDataSource1">
        <ItemTemplate>
          <h4>
            <%# XPath("Name") %>
            (<%# XPath("Name/../@id") %>)</h4>
          <p>
            <asp:DataList ID="pets" runat="server" DataSourceID="XmlDataSource1">
              <ItemTemplate>
                <%# XPath("Student[Name/../@id]/Pets/Pet") %> (<%# XPath("Student[Name/../@id]/Pets/Pet/@type")%>)</ItemTemplate>
            </asp:DataList>
          </p>
        </ItemTemplate>
      </asp:DataList>
    

    The first part of my page displays correctly (where each student's name prints, along with the ID), but the nested DataList isn't populating with anything.  Does anyone have a suggestion on how I can get each pet to list beneath their respective student?

    Monday, July 25, 2011 5:01 PM

Answers

  • Rockstarter,
     
    I guess you need to select all the students in the outter DataList by <%# XPathSelect("Student") %>, then in the inner DataList select all the pets of that student by <%# XPathSelect("Pets/Pet") %> and populate it by a Repeater.
    Please try following code (Please adjust the asp stuff if needed, I know nothing about ASP.Net)
    	<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Students.xml">
    	</asp:XmlDataSource>
    	<asp:DataList ID="students" runat="server" DataSourceID="XmlDataSource1">
     <ItemTemplate>
    		<!-- select all the students -->
    		<asp:Repeater id="Repeater1" runat="server" DataSource='<%# XPathSelect("Student") %>'>
    			<ItemTemplate>	
    				<h4>
    					<%# XPath("Name") %>
    					(<%# XPath("@id") %>)
    				</h4>
    				<p>
    					<!-- select Pets under current student -->
    					<asp:DataList ID="pets" runat="server" DataSource='<%# XPathSelect("Pets/Pet") %>'>
    						<ItemTemplate>
    							<%# XPath(".") %>
    						</ItemTemplate>
    					</asp:DataList>
    				</p>
    			</ItemTemplate>
    		</asp:Repeater>
     </ItemTemplate>
     </asp:DataList>	
    

    and also please refer to http://forums.asp.net/p/1630029/4197406.aspx which does exactly the same thing you want
    Wednesday, August 17, 2011 8:18 AM

All replies

  • So, I read about something called the XPathSelect, which appears to let me set the DataSource to a specific node of my XML Document.  By using the following markup, I was able to get a single pet (the first one) to print beneath the students name.  Not quite what I was going for (since I want ALL to print), but I'm getting closer.  Will post back when I find more.
      <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Students.xml">
      </asp:XmlDataSource>
      <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" >
        <ItemTemplate>
          <h4>
            <%# XPath("Name") %>
            (<%# XPath("Name/../@id") %>)</h4>
            
          <p>
     <asp:DataList ID="DataList2" runat="server" DataSource='<%# XPathSelect("Pets") %>'>
     <ItemTemplate>
      <%# XPath("Pet") %>
     </ItemTemplate>
     </asp:DataList>
          </p>
        </ItemTemplate>
      </asp:DataList>
    

    Monday, July 25, 2011 5:30 PM
  • Friendly bump.  This problem is still sitting on my plate.  :(
    Monday, August 01, 2011 6:11 PM
  • Rockstarter,
     
    I guess you need to select all the students in the outter DataList by <%# XPathSelect("Student") %>, then in the inner DataList select all the pets of that student by <%# XPathSelect("Pets/Pet") %> and populate it by a Repeater.
    Please try following code (Please adjust the asp stuff if needed, I know nothing about ASP.Net)
    	<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Students.xml">
    	</asp:XmlDataSource>
    	<asp:DataList ID="students" runat="server" DataSourceID="XmlDataSource1">
     <ItemTemplate>
    		<!-- select all the students -->
    		<asp:Repeater id="Repeater1" runat="server" DataSource='<%# XPathSelect("Student") %>'>
    			<ItemTemplate>	
    				<h4>
    					<%# XPath("Name") %>
    					(<%# XPath("@id") %>)
    				</h4>
    				<p>
    					<!-- select Pets under current student -->
    					<asp:DataList ID="pets" runat="server" DataSource='<%# XPathSelect("Pets/Pet") %>'>
    						<ItemTemplate>
    							<%# XPath(".") %>
    						</ItemTemplate>
    					</asp:DataList>
    				</p>
    			</ItemTemplate>
    		</asp:Repeater>
     </ItemTemplate>
     </asp:DataList>	
    

    and also please refer to http://forums.asp.net/p/1630029/4197406.aspx which does exactly the same thing you want
    Wednesday, August 17, 2011 8:18 AM