Skip to content

Instantly share code, notes, and snippets.

@douglas-mason
Created November 13, 2013 19:55
Show Gist options
  • Save douglas-mason/7455307 to your computer and use it in GitHub Desktop.
Save douglas-mason/7455307 to your computer and use it in GitHub Desktop.
Grabs value of specified attribute from a site map node. If recursive search is specified, then search up the site map tree for the next ancestor node that contains a value for the specified attribute.
public static string GetNodeAttributeValue(SiteMapNode currentNode, string attributeName, bool recursive)
{
if (recursive) // Do recursive search
{
string attributeValue = "";
if (currentNode != null)
{
attributeValue = GetNodeAttributeValue(currentNode, attributeName, false); // Search node currently working with.
if (string.IsNullOrEmpty(attributeValue)) // If attribute not found, then check the parent node.
{
if (currentNode.ParentNode != null)
{
attributeValue = GetNodeAttributeValue(currentNode.ParentNode, attributeName, true);
}
}
}
return attributeValue;
}
else // Search currentNode only
{
if (currentNode != null && currentNode[attributeName] != null)
{
return currentNode[attributeName];
}
else
{
return ""; // If attribute is not found return empty string.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment