locked
XML Navigation RRS feed

  • Question

  • User172142550 posted

    Hello,

    I am trying to consume an RSS Feed in my website. However, when I launch the page, the datas get mixed up. I would also like to have navigation buttons but really can't figure out how to do it. Please find my codes below:

    Imports System.IO
    Imports System.Xml
    Imports System.Xml.XPath

    Partial Class Default2
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim doc As New System.Xml.XmlDocument
            doc.Load("http://earthquake.usgs.gov/earthquakes/shakemap/rss.xml")

            Dim list = doc.GetElementsByTagName("item")
            Dim i As Integer = 0
            For Each item As System.Xml.XmlElement In list
                Response.Write("Title: " & doc.GetElementsByTagName("title").Item(i).InnerText)
                Response.Write("<br/>Description: " & doc.GetElementsByTagName("description").Item(i).InnerText)
                Response.Write("<br/>Link: " & doc.GetElementsByTagName("link").Item(i).InnerText)
                Response.Write("<br/>Published Date: " & doc.GetElementsByTagName("pubDate").Item(i).InnerText)
                Response.Write("<br/>Latitude: " & doc.GetElementsByTagName("geo:lat").Item(i).InnerText)
                Response.Write("<br/>Longitude: " & doc.GetElementsByTagName("geo:long").Item(i).InnerText)
                Response.Write("<br/>Magnitude: " & doc.GetElementsByTagName("dc:subject").Item(i).InnerText)
                Response.Write("<br/>Duration: " & doc.GetElementsByTagName("eq:seconds").Item(i).InnerText)
                Response.Write("<br/>Depth: " & doc.GetElementsByTagName("eq:depth").Item(i).InnerText)
                Response.Write("<br/>Region: " & doc.GetElementsByTagName("eq:region").Item(i).InnerText)
                Response.Write("<br/><br/>")
                i = i + 1
            Next
        End Sub

    Thursday, August 23, 2012 11:54 PM

Answers

  • User-489377026 posted

    Hi,

    Paging the xml data item, I will recommend data control, like ListView and DataPager control

    Assume you got a simple xml file(sites.xml):

    <?xml version="1.0" encoding="utf-8" ?>
    <sites>
        <site id="1" url="www.Microsoft.com" title="Microsoft1" />
        <site id="2" url="www.asp.net" title="asp.net2" />
        <site id="3" url="www.Microsoft.com" title="Microsoft3" />
        <site id="4" url="www.asp.net" title="asp.net4" />
        <site id="5" url="www.Microsoft.com" title="Microsoft5" />
        <site id="6" url="www.Microsoft.com" title="Microsoft6" />
        <site id="7" url="www.asp.net" title="asp.net7" />
        <site id="8" url="www.Microsoft.com" title="Microsoft8" />
        <site id="9" url="www.Microsoft.com" title="Microsoft9" />
        <site id="10" url="www.Microsoft.com" title="Microsoft10" />
        <site id="11" url="www.Microsoft.com" title="Microsoft11" />
        <site id="12" url="www.Microsoft.com" title="Microsoft12" />
        <site id="13" url="www.Microsoft.com" title="Microsoft13" />
    </sites>

    .aspx file

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>ListView Example</title>
    </head>
    <body>
        <form id="frmListViewExample" runat="server">
        <asp:ListView ID="lvExample" runat="server" DataSourceID="XmlDataSource1">
            <AlternatingItemTemplate>
                <li style="">id:<asp:Literal ID="litID" runat="server" Text='<%# Eval("id") %>' /><br />
                    url:<asp:Literal ID="litURL" runat="server" Text='<%# Eval("url") %>' /><br />
                    title:<asp:Literal ID="litTitle" runat="server" Text='<%# Eval("title") %>' /><br />
                </li>
            </AlternatingItemTemplate>
            <LayoutTemplate>
                <ul>
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                </ul>
            </LayoutTemplate>
            <EmptyDataTemplate>
                No data was returned.
            </EmptyDataTemplate>
            <ItemTemplate>
                <li style="">id:<asp:Literal ID="litID" runat="server" Text='<%# Eval("id") %>' /><br />
                    url:<asp:Literal ID="litURL" runat="server" Text='<%# Eval("url") %>' /><br />
                    title:<asp:Literal ID="litTitle" runat="server" Text='<%# Eval("title") %>' /><br />
                </li>
            </ItemTemplate>
            <ItemSeparatorTemplate>
                <hr>
            </ItemSeparatorTemplate>
        </asp:ListView>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvExample" 
            PageSize="5">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/sites.xml" />
        </form>
    </body>
    </html>

    DataPager can paging the item. You want display each item per page, then set PageSize="1"


    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 27, 2012 2:31 AM
  • User2005163257 posted

    In your rss.xml file

    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" 

    and your has

      Item: <%#XPath("geo:lat")%><br />
    So you need XmlNamespaceManager.

    Take a look this:http://blog.statichippo.com/2009/12/net-xpath-and-xmlnamespacemanager/

    And you might want to try an XPath Visualizer tool to help you through. XPathVisualizer is free, easy to use.

    Refer: http://stackoverflow.com/questions/585812/using-xpath-with-default-namespace-in-c-sharp

    And you want more people help, you should open a new thread, because now your question is different.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2012 10:20 PM

All replies

  • User-1360095595 posted
    Inside your foreach loop, I think you meant to use "item" your interator instead of doc.
    Friday, August 24, 2012 12:42 AM
  • User172142550 posted

    Hi Mate,

    You mean : Response.Write("Title: " & item.GetElementsByTagName("title").Item(i).InnerText)

    Friday, August 24, 2012 1:23 AM
  • User-1360095595 posted
    Something like that. You're iterating through list and I assume you want to display the data for every "item".
    Friday, August 24, 2012 6:16 AM
  • User172142550 posted

    I've tried something else. It just worked. However, I would like to have the data sorted by the pubDate. Do you have an idea how this can be done.

    Here is the sample code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim doc As New System.Xml.XmlDocument
            doc.Load("http://earthquake.usgs.gov/earthquakes/shakemap/rss.xml")

            Dim list = doc.GetElementsByTagName("item")
            Dim i As Integer = 0
            For Each item As System.Xml.XmlElement In list
                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Title: " & doc.GetElementsByTagName("item").Item(i).InnerText)
                Else
                    Response.Write("Title: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Published Date: " & doc.GetElementsByTagName("pubDate").Item(i + 1).InnerText)
                Else
                    Response.Write("Published Date: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Latitude: " & doc.GetElementsByTagName("geo:lat").Item(i).InnerText)
                Else
                    Response.Write("Latitude: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Longitude: " & doc.GetElementsByTagName("geo:long").Item(i).InnerText)
                Else
                    Response.Write("Longitude: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Magnitude: " & doc.GetElementsByTagName("dc:subject").Item(i).InnerText)
                Else
                    Response.Write("Magnitude: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Duration: " & doc.GetElementsByTagName("eq:seconds").Item(i).InnerText)
                Else
                    Response.Write("Duration: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Depth: " & doc.GetElementsByTagName("eq:depth").Item(i).InnerText)
                Else
                    Response.Write("Depth: ")
                End If


                If doc.Equals(Nothing) = False Then
                    Response.Write("<br/>Region: " & doc.GetElementsByTagName("eq:region").Item(i).InnerText)
                Else
                    Response.Write("Region: ")
                End If

                Response.Write("<br/><br/>")
                i = i + 1
            Next

        End Sub

    End Class

    Friday, August 24, 2012 6:36 AM
  • User172142550 posted

    When I'm running this piece of code, all the tsunami information are displaying in a single page. I want to display each tsunami information per page. Please help :-(

    Saturday, August 25, 2012 11:46 PM
  • User-489377026 posted

    Hi,

    Paging the xml data item, I will recommend data control, like ListView and DataPager control

    Assume you got a simple xml file(sites.xml):

    <?xml version="1.0" encoding="utf-8" ?>
    <sites>
        <site id="1" url="www.Microsoft.com" title="Microsoft1" />
        <site id="2" url="www.asp.net" title="asp.net2" />
        <site id="3" url="www.Microsoft.com" title="Microsoft3" />
        <site id="4" url="www.asp.net" title="asp.net4" />
        <site id="5" url="www.Microsoft.com" title="Microsoft5" />
        <site id="6" url="www.Microsoft.com" title="Microsoft6" />
        <site id="7" url="www.asp.net" title="asp.net7" />
        <site id="8" url="www.Microsoft.com" title="Microsoft8" />
        <site id="9" url="www.Microsoft.com" title="Microsoft9" />
        <site id="10" url="www.Microsoft.com" title="Microsoft10" />
        <site id="11" url="www.Microsoft.com" title="Microsoft11" />
        <site id="12" url="www.Microsoft.com" title="Microsoft12" />
        <site id="13" url="www.Microsoft.com" title="Microsoft13" />
    </sites>

    .aspx file

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>ListView Example</title>
    </head>
    <body>
        <form id="frmListViewExample" runat="server">
        <asp:ListView ID="lvExample" runat="server" DataSourceID="XmlDataSource1">
            <AlternatingItemTemplate>
                <li style="">id:<asp:Literal ID="litID" runat="server" Text='<%# Eval("id") %>' /><br />
                    url:<asp:Literal ID="litURL" runat="server" Text='<%# Eval("url") %>' /><br />
                    title:<asp:Literal ID="litTitle" runat="server" Text='<%# Eval("title") %>' /><br />
                </li>
            </AlternatingItemTemplate>
            <LayoutTemplate>
                <ul>
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
                </ul>
            </LayoutTemplate>
            <EmptyDataTemplate>
                No data was returned.
            </EmptyDataTemplate>
            <ItemTemplate>
                <li style="">id:<asp:Literal ID="litID" runat="server" Text='<%# Eval("id") %>' /><br />
                    url:<asp:Literal ID="litURL" runat="server" Text='<%# Eval("url") %>' /><br />
                    title:<asp:Literal ID="litTitle" runat="server" Text='<%# Eval("title") %>' /><br />
                </li>
            </ItemTemplate>
            <ItemSeparatorTemplate>
                <hr>
            </ItemSeparatorTemplate>
        </asp:ListView>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvExample" 
            PageSize="5">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/sites.xml" />
        </form>
    </body>
    </html>

    DataPager can paging the item. You want display each item per page, then set PageSize="1"


    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 27, 2012 2:31 AM
  • User172142550 posted

    Hi Mark,

    Thank you for your answer. This is exactly what i need but I need to call an xml page ("http://earthquake.usgs.gov/earthquakes/shakemap/rss.xml") and i'm not able to do it. Your guidance is much appreciated. Please help me.

    Tuesday, August 28, 2012 2:30 AM
  • User172142550 posted

    This is my piece of code below. I would like to read the geo:lat tag in the XML file but it returns me an error when compiling. Please help

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="frmListViewExample" runat="server">
        <asp:ListView ID="lvExample" runat="server" DataSourceID="XmlDataSource1">
            <AlternatingItemTemplate>
                    Title: <%#XPath("title")%><br />
                    Published Date: <%#XPath("pubDate")%><br />
                    Link: <%#XPath("link")%><br />
                    Description: <%#XPath("description")%>
                    Item: <%#XPath("geo:lat")%><br />
            </AlternatingItemTemplate>
            <LayoutTemplate>
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
            </LayoutTemplate>
            <EmptyDataTemplate>
                No data was returned.
            </EmptyDataTemplate>
            <ItemTemplate>
                    Title: <%#XPath("title")%><br />
                    Published Date: <%#XPath("pubDate")%><br />
                    Link: <%#XPath("link")%><br />
                    Description: <%#XPath("description")%><br />
                    Item: <%#XPath("geo:lat")%><br />
            </ItemTemplate>
            <ItemSeparatorTemplate>
                <hr>
            </ItemSeparatorTemplate>
        </asp:ListView>
        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvExample"
            PageSize="1">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://earthquake.usgs.gov/earthquakes/shakemap/rss.xml"
                XPath="rss/channel/item"></asp:XmlDataSource>
        </form>
    </body>
    </html>

    Tuesday, August 28, 2012 5:22 AM
  • User2005163257 posted

    In your rss.xml file

    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" 

    and your has

      Item: <%#XPath("geo:lat")%><br />
    So you need XmlNamespaceManager.

    Take a look this:http://blog.statichippo.com/2009/12/net-xpath-and-xmlnamespacemanager/

    And you might want to try an XPath Visualizer tool to help you through. XPathVisualizer is free, easy to use.

    Refer: http://stackoverflow.com/questions/585812/using-xpath-with-default-namespace-in-c-sharp

    And you want more people help, you should open a new thread, because now your question is different.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 29, 2012 10:20 PM