views:

515

answers:

2

I have a .cs file that builds an xml document and stores it in a variable.

I want to "inject" it into my <asp:XmlDataSource> so that I do not have to save each xml file (it is different every load). How could I go about this?

In my codebehind cs file, I have:

XmlDocument x = blahblah

XmlDataSource MySource = new XmlDataSource();

MySource.Data = x.InnerXml

On my aspx file I have in a form tag:

<asp:XmlDataSource ID="MySource" runat="server" > </asp:XmlDataSource> <asp:TreeView ID="TreeView1" DataSourceId="MySource" runat="server" />

Yet when I run this I get the easy: Object reference not set to an instance of an object

+2  A: 

If the MySource ID of the XmlDataSource is not being recognized, your problem lies elsewhere. In normal conditions, any runat="server" controls you declare in the ASPX file should be accessible in the code-behind file.

You should check if the ASPX is properly mapped to the code-behind file (@Page declaration).

Additionally, you can directly assign the string XML into the XmlDataSource.Data property without the use of the intermediate variable, because it accepts a string as input.

Cerebrus
A: 

I believe you might have just been missing a call to XmlDataSource's DataBind() method.

MySource.DataBind();

vitaminjeff