Answered by:
XPATH Help

Question
-
User-990300661 posted
I'm trying to consume an RSS feed into a repeater to display the title and an image in a list item. The feed can be seen at http://coastalscience.noaa.gov/news/feed/
Some descriptions have an image, and some do not. What I would like to do is get all nodes (maybe the last 5) that have a description with an image. Example:
<item> <title>Community Leaders Explore Coastal Ocean Science</title> <description> <![CDATA[ <p><img src="###" class="###" alt="###" style="###" /> Regular text appears here</p> <p>The post <a rel="nofollow" href="http://coastalscience.noaa.gov/news/topics/misc/community-leaders-explore-coastal-ocean-science-beaufort-laboratory/">Community Leaders Explore Coastal Ocean Science at Beaufort Laboratory</a> appeared first on <a rel="nofollow" href="http://coastalscience.noaa.gov/news">News and Feature Stories</a>.</p> ]]> </description> </item>
So far, I've figured out how to list only the last 5 items from the following code, but I'm not sure where to go next:
<asp:XmlDataSource id="genericXmlDataSource" runat="server" datafile="http://coastalscience.noaa.gov/news/feed/" XPath="rss/channel/item[position()<6]" > </asp:XmlDataSource>
Can anyone point me in the right direction please?
Wednesday, April 2, 2014 1:33 PM
Answers
-
User697462465 posted
Hi Karls,
The XPath can't find the elements from CDATA fragment, but we can use the Regex to solve it, like this:
<asp:Repeater runat="server" DataSourceID="genericXmlDataSource"> <ItemTemplate> Title:<asp:Label runat="server" Text='<%#XPath("title") %>'></asp:Label><br /> Description:<%# System.Text.RegularExpressions.Regex.Match(XPath("description").ToString(),"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>").Value %><br /> </ItemTemplate> </asp:Repeater>
More information about Regex please refer to:
http://msdn.microsoft.com/en-us/library/ms228595.aspxHope it helps.
Best Regards,
Terry Guo- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, April 4, 2014 1:53 AM
All replies
-
User697462465 posted
Hi Karls,
We can use the <%#XPath("path")%> to bind the xml result to server control, like Repeater,GridView etc.
So, please try to use the following code to display your result:
<asp:Repeater runat="server" DataSourceID="genericXmlDataSource"> <ItemTemplate> Title:<asp:Label runat="server" Text='<%#XPath("title") %>'></asp:Label><br /> Description:<%#XPath("description") %><br /> </ItemTemplate> </asp:Repeater> <asp:XmlDataSource id="genericXmlDataSource" runat="server" datafile="http://coastalscience.noaa.gov/news/feed/" XPath="rss/channel/item[position()<6]" > </asp:XmlDataSource>
More information about XmlDataSource please refer to:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.xmldatasource(v=vs.110).aspxHope it helps.
Best Regards,
Terry GuoThursday, April 3, 2014 2:31 AM -
User-990300661 posted
Hi Terry,
I had that part, but failed to post it, sorry.
<asp:Repeater ID="rptSlider" runat="server" datasourceid="genericXmlDataSource" enableviewstate="false"> <headerTemplate><ul></headerTemplate> <ItemTemplate> <li><asp:hyperlink runat="server" navigateurl='<%#XPath("link")%>' text='<%#XPath("title")%>' /> <img src="##" alt="###"/></li> </ItemTemplate> <footerTemplate></ul></footerTemplate> </asp:Repeater>
What I'm trying to figure out is how to target and retrieve the items where a description has an image in it, and display the image itself in the repeater.
Thanks
Thursday, April 3, 2014 7:44 AM -
User697462465 posted
Hi Karls,
The XPath can't find the elements from CDATA fragment, but we can use the Regex to solve it, like this:
<asp:Repeater runat="server" DataSourceID="genericXmlDataSource"> <ItemTemplate> Title:<asp:Label runat="server" Text='<%#XPath("title") %>'></asp:Label><br /> Description:<%# System.Text.RegularExpressions.Regex.Match(XPath("description").ToString(),"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>").Value %><br /> </ItemTemplate> </asp:Repeater>
More information about Regex please refer to:
http://msdn.microsoft.com/en-us/library/ms228595.aspxHope it helps.
Best Regards,
Terry Guo- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, April 4, 2014 1:53 AM -
User-990300661 posted
Thanks Terry, this helped me figure it out.
Cheers!
Friday, April 4, 2014 8:57 AM