Extension method to umbraco Nodes: Find(string childName)
This extension method makes is possible to find a child node with a specifiq name:
[code]//The extension (Extensions.cs)
namespace CPalm.Umbraco
{
public static class Extensions
{
public static Node Find(this Nodes nodes, string name)
{
foreach (Node node in nodes)
{
if (node.Name.ToLowerInvariant() == name.ToLowerInvariant())
return node;
}
return null;
}
}
}[/code]
[code]// The sample
// Remember to include the using to the namespace where you Extensions is located
using CPalm.Umbraco;
namespace CPalm.masterpages
{
public partial class Test : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Node currentNode = Node.GetCurrent();
// Find the child with the name Config
Node configNode = currentNode.Children.Find("Config");
if (configNode == null)
{
throw new Exception("Could not find child node");
}
}
}
}[/code]
Other extension methods: Extension method to umbraco Node: ToXPathNodeIteratorhttp://bit.ly/baHwn Extension method to umbraco Node: GetPropertyValuehttp://bit.ly/CPalmGPV
These are all great small extension methods. I think many of us has written code to handle similar tasks over and over and over again in different projects. Making extension methods out of them like this does save dev and testing time, and publishing the code here will benefit the community. What I want to say is: Thanks for sharing!
Extension method to umbraco Nodes: Find(string childName)
This extension method makes is possible to find a child node with a specifiq name:
[code]//The extension (Extensions.cs)
namespace CPalm.Umbraco
{
public static class Extensions
{
public static Node Find(this Nodes nodes, string name)
{
foreach (Node node in nodes)
{
if (node.Name.ToLowerInvariant() == name.ToLowerInvariant())
return node;
}
return null;
}
}
}[/code]
[code]// The sample
// Remember to include the using to the namespace where you Extensions is located
using CPalm.Umbraco;
namespace CPalm.masterpages
{
public partial class Test : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Node currentNode = Node.GetCurrent();
// Find the child with the name Config
Node configNode = currentNode.Children.Find("Config");
if (configNode == null)
{
throw new Exception("Could not find child node");
}
}
}
}[/code]
Other extension methods:
Extension method to umbraco Node: ToXPathNodeIterator http://bit.ly/baHwn
Extension method to umbraco Node: GetPropertyValue http://bit.ly/CPalmGPV
Enjoy
These are all great small extension methods. I think many of us has written code to handle similar tasks over and over and over again in different projects. Making extension methods out of them like this does save dev and testing time, and publishing the code here will benefit the community. What I want to say is: Thanks for sharing!
is working on a reply...