Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7413893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:58:22+00:00 2026-05-29T06:58:22+00:00

I’m trying to read in an XML feed using an XSLT Macro in Umbraco

  • 0

I’m trying to read in an XML feed using an XSLT Macro in Umbraco and have it display the content nicely formatted. My Macro works fine when the feed is available, but if the feed returns a 404 I cannot manage to get the XSLT to gracefully handle it.

I’m getting the XML using umbraco.library:GetXmlDocumentByUrl()
I’m finding that it is creating a parse error, and that sometimes it just crashes the site rather than return the error text I have specified.

I have also tried wrapping the GetXmlDocumentByUrl() in a document() test to see if I can use that to handle the error a little better. I have found that whilst this stops the site from crashing, and does work if the XML feed exists, it still creates a parse error rather than displaying my error text.

I’d appreciate any help or advice on this, my code is below:

<xsl:variable name="feed" select="'http://url.to.feed'"/>

<xsl:template match="/">
  <xsl:value-of select="document($feed)"/>
  <!-- start writing XSLT -->
  <xsl:choose>
    <xsl:when test="string-length($feed) > 0 and $feed != ''">
      <xsl:choose>
        <xsl:when test="document($feed)">
           File found
          <xsl:variable name="feedContent" select="umbraco.library:GetXmlDocumentByUrl($feed, $cacheRate)"/>
          <xsl:choose>
            <xsl:when test="count($feedContent/error) &gt; 0">
            <!--<xsl:when test="$feedContent != 'error'">-->
              <p class="feedList">
                <strong>This dynamic content is currently not available</strong><br />
                The content could not be loaded. Please verify that you are on the correct page and that you have an
                active internet connection.
              </p>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="renderFeed">
                <xsl:with-param name="feedContent" select="$feedContent"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
            Can't find the file...
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <p class="feedList">
        <strong>No content exists for this page</strong><br />
        Please view another page.
      </p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Update:
I’ve tried skimming down my code to simplify the issue to the following, this is supposed to use the non-cache implementation of GetXmlDocumentByUrl so that I would ensure that I don’t have an issue there and also output the value straight away to ensure it’s not my choose statments:

 <xsl:template match="/">
  <!-- start writing XSLT -->
  <xsl:choose>
    <xsl:when test="string-length($feed) > 0 and $feed != ''">
      <xsl:variable name="vDoc" select="umbraco.library:GetXmlDocumentByUrl($feed)"/>
      <xsl:value-of select="$vDoc"/>
      <xsl:choose>
        <xsl:when test="$vDoc">
           File found
        </xsl:when>
        <xsl:otherwise>
            Can't find the file...
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>
      <p class="feedList">
        <strong>No content exists for this page</strong><br />
        Please view another page.
      </p>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

With a definite 404 page it returns a string of “System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse() at umbraco.library.GetXmlDocumentByUrl(String Url)” However with the feed that I’m actually having problems with its timing out, I double checked with fiddler and it seems that the page is actually returning a 200, but not an XML document, I should mention that in my renderFeed template is as follows, so I would have still expected it to display the otherwise content rather than timeout.

 <xsl:template name="renderFeed">
  <xsl:param name="feedContent" />
  <xsl:choose>
    <xsl:when test="count($feedContent//item) &gt; 0">
        //Render Feed content
    </xsl:when>
    <xsl:otherwise>
     <p class="feedList">
        <strong>No content exists for this page</strong><br />
        Please view another page.
      </p>
   </xsl:otherwise>
  </xsl:choose>
</xsl:template>

I got the when tests from an example, is there a better way I should be testing for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:58:23+00:00Added an answer on May 29, 2026 at 6:58 am

    The only problem I can see is if your XmlDocument is loading a ContentType of text/html rather than text/xml. I wrote a simple function, based on the original one in Umbraco, to allow you to alter the WebRequest timeout plus, it checks the ContentType.

    public static XPathNodeIterator GetXmlDocumentByUrl(string Url, int requestTimeout = 100000)
    {
        XmlDocument xmlDoc = new XmlDocument();
        WebRequest request = WebRequest.Create(Url);
    
        try
        {
            // Set the Request Timeout
            request.Timeout = requestTimeout;
            using (WebResponse response = request.GetResponse())
            {
                if (response.ContentType.Contains("text/xml"))
                {
                    using (Stream responseStream = response.GetResponseStream())
                    {
                        XmlTextReader reader = new XmlTextReader(responseStream);
                        xmlDoc.Load(reader);
                    }
                }
                else
                    xmlDoc.LoadXml(string.Format("<error url=\"{0}\">Failed to load an ContentType of XML</error>",
                                                 HttpContext.Current.Server.HtmlEncode(Url)));
            }
        }
        catch (WebException err)
        {
            xmlDoc.LoadXml(string.Format("<error url=\"{0}\">{1}</error>",
                                         HttpContext.Current.Server.HtmlEncode(Url), err));
        }
        catch (Exception err)
        {
            xmlDoc.LoadXml(string.Format("<error url=\"{0}\">{1}</error>",
                                         HttpContext.Current.Server.HtmlEncode(Url), err));
        }
    
        XPathNavigator xp = xmlDoc.CreateNavigator();
        return xp.Select("/");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.