Bind XMLDataSource with HTTP Handler

I have dynamically generated XML data that will be consumed by multiple consumers (ASPX page, flash file and maybe another one).

I have implemented it as a custom handler. I create XML in a 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 doesn't call it over HTTP.

Any advice?

+1


a source to share


1 answer


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

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

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

Edit :

Since you are using the XmlTextWriter and from your comment, this seems to be a little confusing. I will add this data.

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

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

      



In your handler, you have this code: -

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

      

Note that there is no need for Response.Write and this makes the encoding done correctly.

In ASP.NET page, you are using: -

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

yourXmlDataSource.Data = source.ToString();

      

The XmlCreator may not need to be specified in which case you could use the static class, it depends on what other data is needed to generate the XM generation.

+3


a source