posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Enumerating the XmlDataSource

Here is the cool piece of code to Enumerate XmlDataSource Tabular/Hierarchical:

You have an XmlDataSource on your page and you wish to enumerate the items as tabular data

 

private void EnumerateDataSource() {

    IDataSource ds = XmlDataSource1 as IDataSource;

    if (ds != null) {

        XmlDataSourceView xmlDsView = ds.GetView(String.Empty) as XmlDataSourceView;

        if (xmlDsView != null) {

            IEnumerator enumerator = xmlDsView.Select(DataSourceSelectArguments.Empty).GetEnumerator();

            while (enumerator.MoveNext()) {

                //Retrieve "Name" attribute from node

                //<Employee ID="4" Name="John" />

                string employeeName = Convert.ToString(DataBinder.Eval(enumerator.Current, "Name"));

                TextArea1.Value += employeeName + "\r\n"; //Append to a TextArea control

            }

        }

    }

}

 

You have an XmlDataSource on your page and you wish to enumerate the items as hierarchical data

 

private void EnumerateHierarchicalDataSource() {

    IHierarchicalDataSource hs = XmlDataSource1 as IHierarchicalDataSource;

    if (hs != null) {

        HierarchicalDataSourceView hsDataSourceView = hs.GetHierarchicalView(String.Empty);

        if (hsDataSourceView != null) {

            IHierarchicalEnumerable iHierarchEnumerable = hsDataSourceView.Select();

            ReadRecursive(iHierarchEnumerable, 0);

        }

    }

}

 

//Recursively read the hierarchical data node

//Quits recursion on current node if HasChildren is not true

private void ReadRecursive(IHierarchicalEnumerable enumerable, int depth) {

    IEnumerator enumerator = enumerable.GetEnumerator();

    while (enumerator.MoveNext()) {

        IHierarchyData hierarchyData = enumerable.GetHierarchyData(enumerator.Current);

        XmlElement element = hierarchyData.Item as XmlElement;

        if (element != null) {

            Textarea2.Value += String.Format("{0} : {1}{2}",

                depth, element.Attributes["Name"].Value,

                Environment.NewLine); //Append to a TextArea control

          

            if (hierarchyData.HasChildren) {

                IHierarchicalEnumerable iHierarchEnumerable = hierarchyData.GetChildren();

                if (iHierarchEnumerable != null) {

                    ReadRecursive(iHierarchEnumerable, depth + 1);

                }

            }

        }

    }

}

 

Source: http://weblogs.asp.net/rajbk/archive/2005/10/18/427743.aspx

 

Print | posted on Wednesday, April 19, 2006 3:54 AM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 3 and 1 and type the answer here:

Powered by: