TreeView Dynamic XML Binding



I'm dynamically binding an XmlDataSource to an xml file in the Page_Load event. This XmlDataSource control is the DataSourceID for a TreeView control. I'm receiving a null reference error prior to entering the Page_Load event when selecting a tree text value.

Page_Load code snippet:
cSchemaXML.DataFile = @"D:\Schemas\2008\Schema_Tree.xml";

Note: The file path above will be calculated once I get it working. This is for testing only. The code is not in a !this.IsPostBack conditional.

Aspx page snippet:
<asp:TreeView ID="cSchemaTree" runat="server" DataSourceID="cSchemaXML" ExpandDepth="1">
</asp:TreeView>
<asp:XmlDataSource ID="cSchemaXML" runat="server"></asp:XmlDataSource>

When I click on the tree node (not the Expand icon) for the select processing, a null reference error (see below) is displayed.
The only way I can get this to work without a the error is to disable client side code:
EnableClientScript="False"

I prefer not to suffer the flashing and time required during postbacks. I'm simply trying to display the xml element tree structure. The xml being displayed is nested elements, each containing one attribute. If I set the DataFile in the XmlDataSource control directly, everything works fine. Do I need to set DataFile someplace other than in the Page_Load event? Some other solution?

Note: If I click a tree node that is part of the initial display (based on the ExpandDepth setting) I do not get the error until I expand the tree and then click the node (or one of the expanded nodes). This may indicate some variance between the viewstate and the returned tree structure (only a guess).

Sample XML - the actual xml is much more complex and nested much deeper, but this gives the idea.

<Company FullPath="Company">
<Name FullPath="Company/Name"/>
<Address FullPath="Company/Address">
<USAddress FullPath="Company/Address/USAddress">
<Street FullPath="Company/Address/USAddress/Street" />
<City FullPath="Company/Address/USAddress/City"/>
<State FullPath="Company/Address/USAddress/State" />
<ZipCode FullPath="Company/Address/USAddress/ZipCode" />
</USAddress>
<ForeignAddress FullPath="Company/Address/ForeignAddress" >
<Street FullPath="Company/Address/USAddress/Street" />
<City FullPath="Company/Address/USAddress/City"/>
<StateOrProvince FullPath="Company/Address/USAddress/StateOrProvince" />
<PostalCode FullPath="Company/Address/USAddress/PostalCode" />
<Country FullPath="Company/Address/ForeignAddress/Country" />
</ForeignAddress>
</Address>
</Company>

The error returned when clicking the tree node is:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +24
System.Xml.XmlDocument.Load(XmlReader reader) +96
System.Web.UI.WebControls.XmlDataSource.PopulateXmlDocument(XmlDocument document, CacheDependency& dataCacheDependency, CacheDependency& transformCacheDependency) +305
System.Web.UI.WebControls.XmlDataSource.GetXmlDocument() +154
System.Web.UI.WebControls.XmlHierarchicalDataSourceView.Select() +14
System.Web.UI.WebControls.TreeView.DataBindNode(TreeNode node) +126
System.Web.UI.WebControls.TreeView.PopulateNode(TreeNode node) +27
System.Web.UI.WebControls.TreeView.LoadPostData(String postDataKey, NameValueCollection postCollection) +1082
System.Web.UI.WebControls.TreeView.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +11
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +661
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1194

Thanks for any help - Rich F.






.



Relevant Pages

  • Re: TreeView Dynamic XML Binding
    ... If I understand correctly, you're trying to create a TreeView based off of an XML file and retain the expand/collapse functionality but AVOID the postbacks and flicker, correct? ... the control generates the according JavaScript and the Expand links do not fire postbacks. ... When I click on the tree node for the select ... Object reference not set to an instance of an object. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Relative XSD path
    ... it fails with the next exception: ... Object reference not set to an instance of an ... with this approach, but I don't want to swamp you with xsd, xml and cs ...
    (microsoft.public.dotnet.languages.csharp)
  • Removing default xmlns from xml
    ... When I try to read xml using SelectSingleNode, I am getting exception ... "Object reference not set to an instance of an object.". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Modeling Data for XML instead of SQL-DBMS
    ... It is a tree, ... A set of XML documents may include ... DBMS tools where the link specifications are metadata. ... but I don't think that is the hierarchy you ...
    (comp.databases.theory)
  • Re: TreeView Dynamic XML Binding
    ... Subject: TreeView Dynamic XML Binding ... Expand the ReturnHeader node. ... When I click on the tree node for the select ... I'm simply trying to display the xml element tree ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)