posts - 236, comments - 436, trackbacks - 56

My Links

News

Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009


I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes www.pageflakes.com.
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta realestatebazaar.com.bd an ASP.NET MVC application written in C#.NET.


Some of 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

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

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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

Feedback

No comments posted yet.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: