Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top