views:

250

answers:

2

Hi

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...

+2  A: 

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:

  XmlDocument document = new XmlDocument();
  XmlNodeList nodes = document.SelectNodes(string.Format("/descendant-or-self::*[@id = '{0}']/*", id));
  foreach (XmlNode child in nodes) {
    // child contains the child node
  }

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:

XDocument doc = new XDocument();
XElement root = doc.Root;
IEnumerable<XElement> children = doc.Root
                                    .DescendantsAndSelf()
                                    .Where(x => (string) x.Attribute("id") == id)
                                    .SelectMany(x => x.Elements());
foreach (var child in children) {
   // do something with the child
}
Obalix
Thank you! I will forget the ASP TreeView and make my own Javascript-based ;-)
m6a-uds
+1  A: 

To find the node you want, recursive search the tree where on start the node is the xmlDoc.DocumentElement.

public XmlNode FindNode( XmlNode node, string id ) {
  XmlNode fNode = null;

  if ( node != null ) {
    bool bfind = id == node.Attributes.GetNamedItem( "Id" ).Value; // here is where you put your matching criteia

    if ( bfind  )
      return node;

    XmlNodeList nodeList = node.ChildNodes;
    int length = node.ChildNodes.Count;

    XmlNode childNode = null;
    int index = 0;
    do {
      childNode = nodeList.Item( index++ );
      fNode = FindNode( childNode, Name );
    } while ( childNode != null && fNode == null );
  }
  return fNode;
}

Once you have the node, you get all of its siblings by using node.NextSibling

JDMX