Attempting to dynamically set a TreeViewNode as selected, my TreeView doesn't contain all the nodes
<p>I'm binding this xml to a TreeView:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;DirectoryNode Id="80e01248-1170-4393-a327-b97409d51159" Title="A"&gt;
   &lt;DirectoryNode Id="e6db6d3f-be30-4cdf-b79e-864cc9b52c5f" Title="A1"&gt;
      &lt;DirectoryNode Id="4368d898-6fb0-4e2f-ba56-edbaf4bd0077" Title="A11" /&gt;
   &lt;/DirectoryNode&gt;
   &lt;DirectoryNode Id="11c5336b-462e-45dc-b4d3-92032ebc3ae3" Title="A2" /&gt;
&lt;/DirectoryNode&gt;
</code></pre>

<p>Here is my TreeView and data source:</p>

<pre><code>&lt;asp:TreeView ID="tvAssets" runat="server" DataSourceID="xdsAssets"&gt;   
    &lt;DataBindings&gt;
        &lt;asp:TreeNodeBinding DataMember="DirectoryNode"
            ValueField="Id" TextField="Title"  /&gt;
    &lt;/DataBindings&gt;
&lt;/asp:TreeView&gt;
&lt;asp:XmlDataSource ID="xdsAssets" runat="server" EnableCaching="false" /&gt;
</code></pre>

<p>Here is my binding code:</p>

<pre><code>protected void BindTree(Guid folderId)
{       
    StringBuilder sb = new StringBuilder();
    XmlWriter xw = XmlWriter.Create(sb);

    GetXmlAndWrite(xw);  // writes xml shown above
    xw.Flush();
    xw.Close();

    xdsAssets.Data = sb.ToString();
    xdsAssets.DataBind();

    tvAssets.DataBind();

    // attempt to set selected node based on folderId
    foreach (TreeNode tn in tvAssets.Nodes)
        if (tn.Value == folderId.ToString())
            tn.Selected = true;
}
</code></pre>

<p>So here's the weird thing: the tree view binds correctly, it displays all the nodes that are in the xml.</p>

<p>But for some reason, none of the nodes are ever being set as selected unless it's the root node. I went through the debugger, and the problem is that my <code>tvAssets.Nodes</code> collection only has one node, the root node. Even though the tree displays correctly on the front end, for some reason that collection only has the root node.</p>

<p>Does anyone know what I'm doing wrong here?</p>
