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.
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 :/
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
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
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.
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
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", ""))); }
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 :
And I've then tried all manor of 'node.Current.Select(xPathStatement)' with no luck. I'm really flying blind here :/
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:
This worked for me hope it helps.
Jon
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
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:
Hi Chad
I used this recently:
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.
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
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.
Slace,
"You can use nodeFactory and (assuming they are the direct children)" - They aren't direct children. The structure looks something like this:
So I'd want it to just grab those 3 nodes.
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.
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:
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:
No dice, it doesn't select any nodes. Here are the following XPath scenarios and their results:
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:
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:
You could try doing this:
is working on a reply...