Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robin Nicholl 137 posts 277 karma points
    Jul 23, 2014 @ 19:58
    Robin Nicholl
    0

    Check if node exists in external RSS feed

    I'm parsing external RSS feed (for train times); main <Service> nodes sometimes contain <Platform> node and sometimes not. If there is one or more <Service> node without a <Platform> node there is an 'Error loading MacroEngine script' and expected results of macro don't display.

    <Service Uid="1">
        <DepartTime time="1831" />
        <Destination1 name="Brighton" />
        <Platform Number="9" />
        <ServiceStatus Status="On Time" />
    </Service>
    <Sefvice Uid="2">
        <DepartTime time="1854" />
        <Destination1 name="Horsham" />
        <ServiceStatus Status="Delayed" />
    </Service>

    How can I check if this node exists so that I don't try displaying non-existent nodes, which makes code fall over?

    Thanks

    Robin

  • Mike Chambers 636 posts 1253 karma points c-trib
    Jul 24, 2014 @ 10:16
    Mike Chambers
    0

    How are you parsing?

    You could  use the umbraco.library:GetXmlDocumentByUrl('URL') [http://our.umbraco.org/wiki/reference/umbracolibrary/getxmldocumentbyurl]

    And once it's xml then use xpath selectsinglenode to determine if the node exists..

    alternatively can't you just wrap your try to display the missing node in a try {}catch{} construct...

    I guess it all depends on the umbraco version and razor/xslt/usercontrol option that you are using,

  • Robin Nicholl 137 posts 277 karma points
    Jul 24, 2014 @ 12:58
    Robin Nicholl
    0

    I have this:

    XmlTextReader departuresRSS = new XmlTextReader("path_to_xml");
    XmlDocument doc = new XmlDocument();
    doc.Load(departuresRSS);
    XmlNodeList rssItems = doc.SelectNodes("//Service");
    <table>
    @{
    foreach (XmlNode node in rssItems) {
    <tr>
    <td>@node["DepartTime"].Attributes["time"].Value</td>
    <td>@node["Platform"].Attributes["Number"].Value</td>
    </tr>
    }
    </table>

    ... but the "Platform" node doesn't always exist (usually, but not always). When it doesn't, the whole thing falls over.

    Thanks

  • Robin Nicholl 137 posts 277 karma points
    Jul 24, 2014 @ 14:27
    Robin Nicholl
    100

    Sorted! This works…

    var plat = node["Platform"] != null ? node["Platform"].Attributes["Number"].Value : "-";
    <td>@plat</td> 

    Thanks for the pointers, Mike.

  • Mike Chambers 636 posts 1253 karma points c-trib
    Jul 24, 2014 @ 14:28
    Mike Chambers
    0

    no worries.. ps the inbuilt library fucntion also accepts a parameter to cache..  in case that appeals (for not have 1000's of users hitting the rss constantly)

  • Robin Nicholl 137 posts 277 karma points
    Jul 24, 2014 @ 15:35
    Robin Nicholl
    0

    Actually that might be of interest: where would I read up on that?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Jul 24, 2014 @ 16:27
    Mike Chambers
    0

    Not sure there is any documentation... I noticed it in the source when I was looking to addess an issue with GetXmlDocumentByUrl() passing a useragent of ie4.. so ending up with issues

     

    https://github.com/umbraco/Umbraco-CMS/blob/d1c5ddc03dfb79195e1c97eededcabc9dcaa1365/src/Umbraco.Web/umbraco.presentation/library.cs

     

    /// <summary>
            /// Gets the XML document by URL Cached.
            /// </summary>
            /// <param name="Url">The URL.</param>
            /// <param name="CacheInSeconds">The cache in seconds (so 900 would be 15 minutes). This is independent of the global cache refreshing, as it doesn't gets flushed on publishing (like the macros do)</param>
            /// <returns></returns>
            public static XPathNodeIterator GetXmlDocumentByUrl(string Url, int CacheInSeconds)
            {
                object urlCache =
                                HttpContext.Current.Cache.Get("GetXmlDoc_" + Url);
                if (urlCache != null)
                    return (XPathNodeIterator)urlCache;
                else
                {
                    XPathNodeIterator result =
                        GetXmlDocumentByUrl(Url);
                    HttpContext.Current.Cache.Insert("GetXmlDoc_" + Url,
                        result, null, DateTime.Now.Add(new TimeSpan(0, 0, CacheInSeconds)), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Low, null);
                    return result;
                }
            }
Please Sign in or register to post replies

Write your reply to:

Draft