locked
Converting datetime from xml- and rss-feed RRS feed

  • Question

  • User-220582354 posted

    We have two different suppliers of newsfeed; one delivers in xml and one in rss. Both have a datetime stamp indicating the articles time of publishing, but obviously in different formats – and none of them are in the format I want to use on our webpage.

    Our site is multilingual, so the format to render varies by culture. Said in code, my desired format is (where MyCulture is given from the database, for instance "en-US" or "nb-NO"):
    .ToString("d", new System.Globalization.CultureInfo(MyCulture, false).DateTimeFormat)

    The xml-feed has the following format:
    <DATE>2008-10-14T21:18:27.0670000+02:00</DATE>

    The rss-feed has the following format:
    <PUBDATE]Wed, 15 Oct 2008 13:11:23 +0200</PUBDATE>

    I have read numerous how-to’s and forum posts about converting datetime, and I have tried a lot of .Parse, .SpecifyKind, .ToFileTime*, .FromFileTime*, To*Date* etc. No matter what I have tried, it always comes down in flames…

    Language is by the way c#.net, and the feeds are hooked up as (two individual instances of) DataList, with the feed as XmlDataSource.

    One example that returns the old, faithful error message:
    <%# ((DateTime) DateTime.FromFileTimeLocal(XPath("Date"))).ToString("d", new System.Globalization.CultureInfo("nb-NO", false).DateTimeFormat)%>

    Can anyone point me in the right direction, or better yet: draw the solution out of the hat?

    Regards.

    Wednesday, October 29, 2008 5:48 AM

Answers

  • User-1038969477 posted

    Hi Horge ,

    FromFileTimeLocal can only accept long type parameter , you can not pass in a string type value.

    Try something like this:

     

       protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lbl = e.Item.FindControl("Label1") as Label;
            if (lbl != null)
            {
                IXPathNavigable nav = e.Item.DataItem as IXPathNavigable;
    
                XPathNavigator xnav = nav.CreateNavigator();
                XPathNodeIterator iter = xnav.Select("DATE");
                while (iter.MoveNext())
                {
                    string str = iter.Current.Name;
                    XPathNavigator attnav = iter.Current.CreateNavigator();
                    attnav.MoveToFirstAttribute();
                    string atti = attnav.Value;
    
                    DateTime time = DateTime.Parse(atti);
                    string res = time.ToString("d", new System.Globalization.CultureInfo("nb-NO", true));
    
                    lbl.Text = res;
      
                }
    
                Label lbl2 = e.Item.FindControl("Label2") as Label;
    
                XPathNavigator xnav2 = nav.CreateNavigator();
                XPathNodeIterator iter2 = xnav2.Select("PUBDATE");
                while (iter2.MoveNext())
                {
                    string str = iter2.Current.Name;
                    XPathNavigator attnav2 = iter2.Current.CreateNavigator();
                    attnav2.MoveToFirstAttribute();
                    string atti2 = attnav2.Value;
    
                    DateTime time2 = DateTime.Parse(atti2);
                    string res2 = time2.ToString("d", new System.Globalization.CultureInfo("nb-NO", true));
    
                    lbl2.Text = res2;
    
                }
            }
        }
      
        <form id="form1" runat="server">
            <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile14.xml"
                XPath="//item"></asp:XmlDataSource>
            <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" OnItemDataBound="DataList1_ItemDataBound" Width="138px">
               <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                   <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
               </ItemTemplate>
            </asp:DataList>
        </form>
      
    <item>
      <DATE>2008-10-14T21:18:27.0670000+02:00</DATE>
      <PUBDATE>Wed, 15 Oct 2008 13:11:23 +0200</PUBDATE>
    </item>
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 4, 2008 2:37 AM

All replies

  • User-1038969477 posted

    Hi Horge ,

    FromFileTimeLocal can only accept long type parameter , you can not pass in a string type value.

    Try something like this:

     

       protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Label lbl = e.Item.FindControl("Label1") as Label;
            if (lbl != null)
            {
                IXPathNavigable nav = e.Item.DataItem as IXPathNavigable;
    
                XPathNavigator xnav = nav.CreateNavigator();
                XPathNodeIterator iter = xnav.Select("DATE");
                while (iter.MoveNext())
                {
                    string str = iter.Current.Name;
                    XPathNavigator attnav = iter.Current.CreateNavigator();
                    attnav.MoveToFirstAttribute();
                    string atti = attnav.Value;
    
                    DateTime time = DateTime.Parse(atti);
                    string res = time.ToString("d", new System.Globalization.CultureInfo("nb-NO", true));
    
                    lbl.Text = res;
      
                }
    
                Label lbl2 = e.Item.FindControl("Label2") as Label;
    
                XPathNavigator xnav2 = nav.CreateNavigator();
                XPathNodeIterator iter2 = xnav2.Select("PUBDATE");
                while (iter2.MoveNext())
                {
                    string str = iter2.Current.Name;
                    XPathNavigator attnav2 = iter2.Current.CreateNavigator();
                    attnav2.MoveToFirstAttribute();
                    string atti2 = attnav2.Value;
    
                    DateTime time2 = DateTime.Parse(atti2);
                    string res2 = time2.ToString("d", new System.Globalization.CultureInfo("nb-NO", true));
    
                    lbl2.Text = res2;
    
                }
            }
        }
      
        <form id="form1" runat="server">
            <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLFile14.xml"
                XPath="//item"></asp:XmlDataSource>
            <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1" OnItemDataBound="DataList1_ItemDataBound" Width="138px">
               <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                   <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
               </ItemTemplate>
            </asp:DataList>
        </form>
      
    <item>
      <DATE>2008-10-14T21:18:27.0670000+02:00</DATE>
      <PUBDATE>Wed, 15 Oct 2008 13:11:23 +0200</PUBDATE>
    </item>
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, November 4, 2008 2:37 AM
  • User-220582354 posted

    Thank you! That was the push we needed to get it working.

    The final code we ended up with was this:

    (DateTime.Parse(XPath("Date").ToString())).ToString("d", new System.Globalization.CultureInfo(MyCulture, true))

    Thursday, November 6, 2008 6:36 AM