XPath, XmlDataSource and selecting multiple elements of a node.
-
Monday, July 25, 2011 5:01 PM
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?
All Replies
-
Monday, July 25, 2011 5:30 PM
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, August 01, 2011 6:11 PMFriendly bump. This problem is still sitting on my plate. :(
-
Wednesday, August 17, 2011 8:18 AM
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- Proposed As Answer by Hubery YuanMicrosoft Employee Wednesday, August 17, 2011 8:25 AM
- Unproposed As Answer by Hubery YuanMicrosoft Employee Wednesday, August 17, 2011 8:25 AM
- Proposed As Answer by Allen ZhangMicrosoft Employee, Moderator Wednesday, August 24, 2011 2:17 AM
- Marked As Answer by Lionello LunesuMicrosoft Employee, Moderator Wednesday, September 07, 2011 3:10 AM

