Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

IHierarchyData Interface

Exposes a node of a hierarchical data structure, including the node object and some properties that describe characteristics of the node. Objects that implement the IHierarchyData interface can be contained in IHierarchicalEnumerable collections, and are used by ASP.NET site navigation and data source controls.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
public interface IHierarchyData
public interface IHierarchyData
public interface IHierarchyData
Not applicable.

The IHierarchyData interface is implemented by classes that represent nodes of a hierarchical structure, and track the hierarchical relationships to their parent and child nodes. Classes that implement the IHierarchyData interface can be contained in collections that implement the IHierarchicalEnumerable interface.

The following code example demonstrates how to implement the IHierarchyData interface with a class that wraps a FileSystemInfo object. The FileSystemInfo class is a good example of a hierarchical data node, which the IHierarchyData interface represents for ASP.NET hierarchical data source controls. This code example is part of a larger example provided for the HierarchicalDataSourceControl class.

public class FileSystemHierarchyData : IHierarchyData
{
    public FileSystemHierarchyData (FileSystemInfo obj) {
        fileSystemObject = obj;
    }

    private FileSystemInfo fileSystemObject = null;

    public override string ToString() {
        return fileSystemObject.Name;
    }
    // IHierarchyData implementation.
    public bool HasChildren {
        get {
            if (typeof(DirectoryInfo) ==  fileSystemObject.GetType()   ) {
                DirectoryInfo temp = (DirectoryInfo) fileSystemObject;
                return (temp.GetFileSystemInfos().Length > 0);
            }
            else return false;
        }
    }
    // DirectoryInfo returns the OriginalPath, while FileInfo returns
    // a fully qualified path.
    public string Path {
        get {
            return fileSystemObject.ToString();
        }
    }
    public object Item {
        get {
            return fileSystemObject;
        }
    }
    public string Type {
        get {
            return "FileSystemData";
        }
    }
    public IHierarchicalEnumerable GetChildren() {
        FileSystemHierarchicalEnumerable children =
            new FileSystemHierarchicalEnumerable();

        if (typeof(DirectoryInfo) == fileSystemObject.GetType()) {
            DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
            foreach (FileSystemInfo fsi in temp.GetFileSystemInfos()) {
                children.Add(new FileSystemHierarchyData(fsi));
            }
        }
        return children;
    }

    public IHierarchyData GetParent()
    {
        FileSystemHierarchicalEnumerable parentContainer =
            new FileSystemHierarchicalEnumerable();

        if (typeof(DirectoryInfo) == fileSystemObject.GetType())
        {
            DirectoryInfo temp = (DirectoryInfo)fileSystemObject;
            return new FileSystemHierarchyData(temp.Parent);
        }
        else if (typeof(FileInfo) == fileSystemObject.GetType())
        {
            FileInfo temp = (FileInfo)fileSystemObject;
            return new FileSystemHierarchyData(temp.Directory);
        }
        // If FileSystemObj is any other kind of FileSystemInfo, ignore it.
        return null;
    }
}

The following code example demonstrates how to recursively iterate through an IHierarchicalEnumerable collection, extract the IHierarchyData item from the enumerator using the GetHierarchyData method, and perform basic work with the data item.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ihd_1.aspx.cs" Inherits="ihd_1_aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

<%@ Page Language="VJ#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private void Page_Load(Object sender, System.EventArgs e)
    {
        IHierarchicalEnumerable ihe = 
            (IHierarchicalEnumerable)SiteMap.get_RootNode().get_ChildNodes();
        IEnumerator enumeration = ihe.GetEnumerator();

        while (enumeration.MoveNext()) {
            // Print out SiteMapNode Titles.
            IHierarchyData hierarchicalNode = 
                ihe.GetHierarchyData(enumeration.get_Current());
            PrintFullChildNodeInfo(hierarchicalNode);
        }
    }//Page_Load
    // Print out the the current data node, then iterate through its
    // children and do the same.
    private void PrintFullChildNodeInfo(IHierarchyData node)
    {
        String whitespace = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        String br = "<br />";

        get_Response().Write(node.toString() + br);
        get_Response().Write(whitespace + node.get_Path() + br);

        // Check for specific types and perform extended functions.
        if (node.get_Type().Equals("SiteMapNode")) {
            // Because SiteMapNode implements the IHierarchyData interface,
            // the IHierarchyData object can be cast directly as a SiteMapNode,
            // rather than accessing the Item property for the object that
            // the Type property identifies.
            SiteMapNode siteNode = null;

            siteNode = (SiteMapNode)node.get_Item();
            get_Response().Write(whitespace + siteNode.get_Url() + br);
            get_Response().Write(whitespace + siteNode.get_Description() + br);
        }
        else {
            if (node.get_Type().Equals("SomeBusinessObject")) {
                // If the IHierarchyData instance is a wrapper class on a
                // business object of some kind, you can retrieve the business
                // object by using the IHierarchyData.Item property.
                // SomeBusinessObject busObj = node.Item as SomeBusinessObject;
            }
        }
        if (node.get_HasChildren()) {
            IEnumerator children = 
                ((IHierarchicalEnumerable)node.GetChildren()).GetEnumerator();

            while (children.MoveNext()) {
                // Print out SiteMapNode Titles recursively.
                IHierarchyData hierarchicalNode = 
                    node.GetChildren().GetHierarchyData(children.get_Current());
                PrintFullChildNodeInfo(hierarchicalNode);
            }
        }
    }//PrintFullChildNodeInfo
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
        <form id="form1" runat="server">

        </form>
    </body>
</html>

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.