Copied to clipboard

Flag this post as spam?

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


  • Simon Osborne 108 posts 150 karma points
    Feb 24, 2012 @ 23:29
    Simon Osborne
    0

    Return list of Nodes from xpath expression from code

    Hi,

    I need to retrieve a list of nodes in a custom section I am writing (in VB.Net) but cannot see an easy way of doing this.

    Library.GetXmlNodeByXPath only seems to return a single node, and umbraco.content.Instance.XmlContent is an issue due to it being ambiguous when used in VB (as discussed here)

    cheers,

     

    SImon

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Feb 25, 2012 @ 00:36
    Hendy Racher
    0

    Hi Simon,

    Have you seen the GetNodesByXPath method in uQuery ? This should do what you want, and returns a List<Node>.

    HTH,

    Hendy

  • Simon Osborne 108 posts 150 karma points
    Feb 25, 2012 @ 01:29
    Simon Osborne
    0

    Thanks Hendy,

    I didn't ideally want to depend on installing an additional package if possible, but I have looked at the source of that uQuery method before for inspiration, but it uses the umbraco.content.Instance.XmlContent method, which unfortunately in VB you causes the ambiguous issue.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Feb 25, 2012 @ 14:19
    Hendy Racher
    0

    Hi Simon,

    Sorry about that, if your XPath will work with a flat xml structure can I offer the following ?

    public static List<Node> GetNodesByXPathViaSql(string xPath)
    {
        var nodes = new List<Node>();
        var xmlDocument = uQuery.GetPublishedXml(UmbracoObjectType.Document);
        var xPathNavigator = xmlDocument.CreateNavigator();
        var xPathNodeIterator = xPathNavigator.Select(xPath);

        while (xPathNodeIterator.MoveNext())
        {
            var node = uQuery.GetNode(xPathNodeIterator.Current.Evaluate("string(@id)").ToString());
            if (node != null)
            {
                nodes.Add(node);
            }
        }

        return nodes;
    }

    The reason that the xml is flat is that it queries the cmlContentXml table and returns all xml fragments for every node, and simply joins them together - but I reckon this still could be quicker than navigating the object model.

    HTH,

    Hendy

  • Simon Osborne 108 posts 150 karma points
    Feb 27, 2012 @ 23:58
    Simon Osborne
    0

    Hi Hendy,

    the only issue would be that I would have to install a 3rd party package to get the uQuery library, which is what I was trying to avoid.

    Its suprised me how tough this is turning out to be, bearing in mind that Umbraco node structure is xml based and thats one of its great strengths.

    Simon

     

Please Sign in or register to post replies

Write your reply to:

Draft