Copied to clipboard

Flag this post as spam?

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

  • Paul Hulatt 47 posts 59 karma points
    Aug 12, 2009 @ 14:42
    Paul Hulatt

    Import Remote CSV in XSLT

    Does anyone know of an easy way that I can import a remote CSV file via XSLT?  I have tried using GetXmlDocumentByUrl but it (quite correctly) errors as the file being loaded is not an XML document.  IS there an equivalent for loading non-XML documents from remote locations.

    Cheers in advance



  • Bert 128 posts 251 karma points
    Aug 12, 2009 @ 14:45

    As far as I know, no.

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 12, 2009 @ 14:48
    Thomas Höhler

    Write your own xslt extension which loads the csv file into an XmlDocument and returens an XmlNodeIterator.

    If you have questions I can give you an example


  • Richard Soeteman 3722 posts 11521 karma points MVP
    Aug 12, 2009 @ 14:58
    Richard Soeteman

    Hi Paul,

    GetXmlDocumentByUrl  is only for Xml. If you just want to import a csv file you could use umbImport. I've build a csv option, so you can select a csv file. This is not a remote file.

    If you need to access the file dynamicly. You can create an xslt extension that returns the data as an xpathNodeIterator.

    Hope this helps you,



  • Thomas Höhler 1237 posts 1709 karma points MVP
    Aug 12, 2009 @ 14:59
    Thomas Höhler

    Something like that will do it:

    public static XPathNodeIterator GetCsv(string filename)
    XmlDocument xdoc = new XmlDocument();
    XmlElement rows = xdoc.CreateElement("rows");

    using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))
    String line;
    // Read and display lines from the file until the end of
    // the file is reached.
    while ((line = sr.ReadLine()) != null)
    XmlElement row = xdoc.CreateElement("row");
    foreach (var item in line.Split(";".ToCharArray()))
    XmlElement col = xdoc.CreateElement("col");
    col.Value = item;
    XPathNavigator xp = xdoc.CreateNavigator();
    return xp.Select("/rows");

    hth, Thomas

  • Lee Kelleher 3890 posts 14696 karma points MVP 9x admin c-trib
    Aug 12, 2009 @ 15:19
    Lee Kelleher

    Following on from Thomas' reply, (at present) a custom method is the only way to go.

    If you need a quick-and-ready solution to this, and don't want to get involved with Visual Studio (God knows why? We all love it), you can do this all natively in XSLT ... using a custom function (see Extend your XSLT with custom functions wiki page)

    Here's an example:

    <?xml version="1.0" encoding="UTF-8"?>
    exclude-result-prefixes="msxml umbraco.library bodenko">
    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <msxml:script implements-prefix="bodenko" language="C#">
    <msxml:assembly name="System" />
    <msxml:assembly name="umbraco" />
    <msxml:using namespace="System.Net" />
    <msxml:using namespace="umbraco" />
    public static XPathNodeIterator GetCsvByUrl(string url, string separator)
    String csv = String.Empty;
    using (WebClient client = new WebClient())
    csv = client.DownloadString(url);
    return umbraco.library.Split(csv, separator);

    <xsl:template match="/">
    <xsl:variable name="csv" select="bodenko:GetCsvByUrl('', ',')" />
    <xsl:if test="count($csv/value) &gt; 0">
    <xsl:for-each select="$csv/value">
    <xsl:value-of select="." />


    The "GetCsvByUrl" method doesn't take multi-line (rows) into account... you can quite easily replace this with Thomas' method (above).

  • Paul Hulatt 47 posts 59 karma points
    Aug 12, 2009 @ 16:29
    Paul Hulatt

    Cheers Lee, that was just the sort of start I was hoping for now I can concentrate of dealing with the actual data and displaying it in some kind on meaningful way.

    Richard, thanks for your reply regarding umbImport, I've used your fantastic package already when migrating an existing site over to Umbraco so thanks for all your hard work.


Please Sign in or register to post replies

Write your reply to: