This is a migrated thread and some comments may be shown as answers.

[Solved] transforming xml into a tree

2 Answers 141 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ken Payson
Top achievements
Rank 1
Ken Payson asked on 13 Sep 2010, 09:52 PM
I have some xml representing a table of contents for marketing reports.  the xml has data in some different attributes depending on the node name.  So, a little custom parsing is necessary.

The xml has three kinds of nodes
 "table-of-contents" (this is the root node)
"section" nodes  (can contain child section nodes or table nodes)
"table" nodes"  

I want to transform this xml into a tree view:

This is what I have but I'm having trouble with the syntax and the arguments to the BindTo method.  Please help me fix it up.  I think that I must be close.

          var toc = @"
                <table-of-content>
                    <section section-id=""S1"" title=""Introduction""></section>
                    <section section-id=""S2"" title=""Lorem Ipsum"">
                        <section section-id=""S2.1"" title=""blah blah""></section>
                        <table table-id=""T1"" title=""earnings per year""></table>
                    </section>                                                                                         
                </table-of-content>"; 
        var root = XElement.Parse(toc);       //var root = XElement.Parse(Model.TableOfContents)); .

        Html.Telerik().TreeView()
                .Name("TreeView")
                .BindTo(root.Elements, mappings => mappings.For<XElement>(binding =>
                    binding.ItemDataBound((item, node) =>
                    {
                        if(node.Name == "section") 
                        {
                            item.Text = node.Attribute("section-id").Value.Substring(1);  //chop off first char
                        }
                        else if (node.Name == "table")
                        {
                            item.Text = "table " + node.Attribute("table-id").Value.Substring(1);  //chop off first char
                        }
                        else
                        {
                            item.Text = "Table of Contents";
                        }
                    })
                    .Children(node => node.Elements)
                    )).Render(); 

2 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 14 Sep 2010, 09:02 AM
Hi,

The code you posted looks at first glance OK. Maybe it is just me, but I could not understand what your problem exactly is. What do you mean "I'm having trouble with the syntax and the arguments to the BindTo method" ?

Regards,
Peter
0
Ken Payson
Top achievements
Rank 1
answered on 15 Sep 2010, 04:28 PM
I solved the problem.  It was just the simplest of syntax errors.  Xml.Linq.XElement.Elements is a function.  I was missing the ()  When I was binding to Elements instead of Elements() I got strange errors that looked to be caused by something bigger.
Tags
TreeView
Asked by
Ken Payson
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
Ken Payson
Top achievements
Rank 1
Share this question
or