c# - Linq to XML Read and output XML generated from lookup list -
i trying use xml created lookup list in sharepoint datasource treeview. in form of :
<newdataset> <test_data> <id>1</id> <title>menuitem_1</title> <child_of /> </test_data> <test_data> <id>2</id> <title>subitem_1</title> <action>http://www.google.com</action> <child_of>menuitem_1</child_of> </test_data> <test_data> <id>3</id> <title>subitem_2</title> <action>http://www.google.com</action> <child_of>menuitem_1</child_of> </test_data> <test_data> <id>4</id> <title>menuitem_2</title> <child_of /> </test_data> <test_data> <id>5</id> <title>subitem_2_1</title> <action>http://www.google.com</action> <child_of>menuitem_2</child_of> </test_data> <test_data> <id>6</id> <title>subitem_2_2</title> <action>http://www.google.com</action> <child_of>menuitem_2</child_of> </test_data> <test_data> <id>7</id> <title>subitem_2_2_1</title> <action>http://www.google.com</action> <child_of>subitem_2_2</child_of> </test_data> </newdataset>
there may n tiers, items relate parent via <child_of>
element. can't seem figure out how write linq in c# nest menu items properly. friend recommended post here. appreciated.
you this, think long putting production code.
from n in testdata.elements("test_data") .aggregate(new dictionary<string, treenode<string>>() {{ string.empty, new treenode<string>() }}, (d, e) => { var curnode = new treenode<string>(e.element("id").value); d.add(e.element("title").value, curnode); d[e.element("child_of").value].addchild(curnode); return d; }) .values n.parent == null select n;
note assumes element has parent key of "empty string" or comes in source list after parent.
personally, factor (especially aggregation) method of own.
Comments
Post a Comment