Copied to clipboard

Flag this post as spam?

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


  • Chad 65 posts 129 karma points c-trib
    Oct 20, 2009 @ 08:47
    Chad
    0

    Convert this xslt macro to c# .net

    Hi guys.

    I've currently got a xslt marco that I am using to create a html select list. Requirements have changed that I need some extra functionality so I've decided to move this to a .net user control.

    The xslt macro just loops over:

    mbraco.library:GetXmlNodeById(1226)/node/child::node [@nodeTypeAlias = 'Paint']

    I'm wondering how I can then get the same functionality over in a .net user control. This is my first .net site and I'm not working on this locally, so can't attach the debugger to browse the objects returned by the umbraco api.

    I've basically got something to the effect of :

    var node = umbraco.library.GetXmlNodeById(NodeID); // gets me the node

    And I've then tried all manor of 'node.Current.Select(xPathStatement)' with no luck. I'm really flying blind here :/

  • webmonger 130 posts 285 karma points
    Oct 20, 2009 @ 17:50
    webmonger
    0

    Hey

    I've just been working with the same problem and found that I was using the wrong method.

    The code you have does not get a node it seems. I googled the problem a little further and found this post http://forum.umbraco.org/yaf_postst5831_Get-properties-out-of-a-document-selected-by-contentpicker.aspx and Dirk say to get the node you use:

    umbraco.presentation.nodeFactory.Node n = new umbraco.presentation.nodeFactory.Node(idOfNode);

    This worked for me hope it helps.

    Jon

  • Chad 65 posts 129 karma points c-trib
    Oct 21, 2009 @ 04:43
    Chad
    0

    Thanks webmonger. That nodeFactory method (at least from looking at it) returns me a single node. Which isn't quite what I'm after. I need to be able to use XPath to select all the appropriate nodes in the tree, as I'm after all the children nodes that are of a particular @nodeTypeAlias

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 21, 2009 @ 04:50
    Aaron Powell
    1

    You can use nodeFactory and (assuming they are the direct children) use LINQ (or a for loop) to get the children of a particular type:

    var node = new Node(1234);
    var children = node.Children.Where(child => child.NodeTypeAlias == "WhatImLookingFor");
  • Claire Botman 48 posts 77 karma points
    Oct 21, 2009 @ 04:52
    Claire Botman
    0

    Hi Chad

    I used this recently:

               Nodes artworks = new Nodes();
                DocumentType dtArtwork = DocumentType.GetByAlias("Artwork");
                umbraco.cms.businesslogic.Content[] c = Document.getContentOfContentType(dtArtwork);

    Then looped through c adding nodes to artworks using artworks.Add(new Node(c[i].Id)); based on property values (c[i].getProperty("").Value)

    I'm not sure if this is the right way to go but it works.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 21, 2009 @ 05:02
    Aaron Powell
    0

    Claire - You'll want to be careful with that, as it can be quite database intensive. the Document API uses the DB, and the ctor hits the DB 2 or 3 times for each construction

  • Claire Botman 48 posts 77 karma points
    Oct 21, 2009 @ 05:09
    Claire Botman
    0

    I was wondering about that at the time but I'm new to this too & didn't know you could do that.  Thankyou for showing us.

  • Chad 65 posts 129 karma points c-trib
    Oct 21, 2009 @ 05:29
    Chad
    0

    Slace,

    "You can use nodeFactory and (assuming they are the direct children)" - They aren't direct children. The structure looks something like this:

    -Main Section Node
    -Sub Section
    -Node I want
    -Node I want
    -Sub Section
    -Node I want
    -Sub Section
    - Node I DON'T want (is a different type)

    So I'd want it to just grab those 3 nodes.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 21, 2009 @ 06:43
    Aaron Powell
    1

    Chad - you could write a recursive function to do it then and pass in the current level and the nodeTypeAlias that you want, looking at the children of it recursively.

  • Chad 65 posts 129 karma points c-trib
    Oct 21, 2009 @ 08:47
    Chad
    0

    Should a recursive function really be needed? The umbraco api returns xml, .net has XPath... it shouldn't be that hard :)

    I've got this:

    var parentNode = umbraco.library.GetXmlNodeById(NodeID);
    parentNode.MoveNext(); // moves to the first node;
    var foo = parentNode.Current.SelectDescendants("node", "", false);

    That returns all the descendants of that top level node. Perfect, now I just want those which have a certain node type alias.... So I change the XPath query to:

    var foo = parentNode.Current.SelectDescendants("node [@nodeTypeAlias = 'MyType']", "", false);

    No dice, it doesn't select any nodes. Here are the following XPath scenarios and their results:

    "*" // returns all nodes and their child data nodes
    data // returns just the data nodes
    node // returns just the nodes

    // All of the above is pretty standard...

    @* // thought it would return all the attributes - no results
    node [1=1] // just trying some stuff - no results
    node/data // no results - now I'm getting confused
  • Chad 65 posts 129 karma points c-trib
    Oct 26, 2009 @ 04:50
    Chad
    0

    Success!

    My issue, is from not reading the docs. the SelectDescendants method does not take an XPath string, it takes a string of the descendant nodes you would like to select.

    So doing the following works fine:

    var node= umbraco.library.GetXmlNodeById(NodeID).Current.Select("node/child::node [@nodeTypeAlias = 'Paint']");

    And then you can interate over that.

    I however, needed to add sorting to the mix as well. So here is what my solution ended up looking like:

    //get the node I'm after
    var node = umbraco.library.GetXmlNodeById(NodeID);

    //I then create a xpathnavigator...
    var navi = node.Current.CreateNavigator();

    //and use that xpathnavigator to create xpathexpression
    var expr = navi.Compile("node/child::node [@nodeTypeAlias = 'Paint']");

    //add sorting to the mix
    expr.AddSort("@nodeName", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);

    //and finally run that expression to select the new nodes in the order I need.
    var sortedNodes = node.Current.Select(expr);

    //loop through and build up my dropdownlist control.
    while (sortedNodes.MoveNext()) {
    ddlPaints.Items.Add(new ListItem(sortedNodes.Current.GetAttribute("nodeName", ""), sortedNodes.Current.GetAttribute("id", "")));
    }
  • arviman 71 posts 92 karma points
    Jun 22, 2010 @ 01:59
    arviman
    0

    You could try doing this:


    //given variables startNodeId and doctypename
    string start = startNodeId > 0 ? string.Format("//node[@id = {0}]", startNodeId) : "";
    System.Xml.XPath.XPathNodeIterator it = umbraco.library.GetXmlNodeByXPath(start + "//node[@nodeTypeAlias = '" + doctypename+ "']");
    while (it.MoveNext()){ //iterate through each node }
Please Sign in or register to post replies

Write your reply to:

Draft