views:

289

answers:

1

Hi

I have some dynamically generated XML data that will be consumed by a few consumers (An ASPX page, a flash file and maybe another one as well).

I have implemented it as a custom handler. I construct the XML in the handler and output it using response.write.

Now If I set the DataFile property of my XMLDataSource to the handler, it tries to read the ashx file literally and does not call it through HTTP.

Any advice?

+2  A: 

Place your XML generation code in a separate class. Have the handler use the class to create the XML and send the results to the client (BTW, don't use Response.Write what XML document tech are you using to build the XML in the first place?).

Make sure the class can expose the completed XML as a string.

In your ASPX page use the same class and assign the XML string to the data property of your XmlDataSource control.

Edit:

Since you are using a XmlTextWriter and from your comment the above is apparently a bit confusing I'll add these details.

You need to take the code that currently generates the XML and place it in a class in the App_Code folder (or possibly in a dll project). This class will have a method that takes as a parameter an XmlTextWriter.

public class XmlCreator
{
    public void GenerateXml(XmlTextWriter writer)
    {
         //All your code that writes the XML
    }
}

In your handler you then have this code:-

XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
XmlCreator creator = new XmlCreator();
XmlCreator.GenerateXml(writer);

Note no need for Response.Write and this gets the encoding done properly.

In your ASP.NET page you use:-

StringWriter source = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(source, Encoding.Unicode);
XmlCreator creator = new XmlCreator();
XmlCreator.GenerateXml(writer);

yourXmlDataSource.Data = source.ToString();

XmlCreator may not need to be instanced in which case you could use a static class it depends on what other data is need to feed the XM generation.

AnthonyWJones
I am using XMLTextWriter for XML generation. I cannot make a class because there will be other consumers of this data than ASP.Net. It has to be something that is accessible from the web, and hence the HttpHandler
Midhat
You can make a class. Your serverside code accesses the XML directly via the class. OTH your client-side code accesses the XML via the HttpHandler which in turn uses the same class to actually create the XML. The code to generate the XML lives in one place (the class) and the HttpHandler becomes an adapter to allow clientside acess to it.
AnthonyWJones
I intended to use the XML as an input to a XMLDataSource. But now I think I would just use the Data property instead of DataFile Property
Midhat