I have a large XML file (3000+ nodes) that I want to represent in a TreeView on ASP.NET. I cannot databind it to a XMLDataSource because loading the TreeView will then be way too slow (I never even waited long enough to see it finish…)
So the solution for this would be to use the PopulateOnDemand property of the TreeNodes to load data only when needed. Problem is, I can’t think of a way to acheive this…
How can-I, based on the ID of a node, search a XMLDocument to get all the childnodes of the node having this ID?
XML would look like that:
<document ID=1>
<document ID=2>
<document ID=3>
</document>
</document>
<document ID=4>
</document>
</document>
There are nor rules on how much levels it can go down or anything…
From my own experience, adding 3000 nodes to an ASP.NET tree view is a bad thing to do, as the viewstate will go berserk … meaning it will explode and become really big!
Try something with clientside Javascript (with JQuery for example) and AJAX requests to speed up loading. Using the MVC and the client side Javascript may also be an option.
Also from experience, do not use MS Ajax to update the treeview, it is not supported.
For reading XML Linq to XML might be an option that’s quire fast. Here you can find a sample for that.
If you have to use System.Xml then the following code gets the child item for the given id from the document:
This works fine when you don’t have namespaces, otherwise, it gets a bit messy.
Using Linq to XML the whole thing looks like this: