将 XMLDataSource 绑定到 HTTP 处理程序

发布于 2024-07-21 04:32:52 字数 239 浏览 16 评论 0原文

我有一些动态生成的 XML 数据,将由一些使用者使用(一个 ASPX 页面、一个 flash 文件,也许还有另一个)。

我已将其实现为自定义处理程序。 我在处理程序中构造 XML 并使用 response.write 输出它。

现在,如果我将 XMLDataSource 的 DataFile 属性设置为处理程序,它会尝试按字面意思读取 ashx 文件,而不是通过 HTTP 调用它。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如痴如狂 2024-07-28 04:32:52

将 XML 生成代码放在单独的类中。 让处理程序使用该类来创建 XML 并将结果发送到客户端(顺便说一句,不要使用 Response.Write 您首先使用什么 XML 文档技术来构建 XML?)。

确保该类可以将完整的 XML 作为字符串公开。

在 ASPX 页面中使用相同的类并将 XML 字符串分配给 XmlDataSource 控件的数据属性。

编辑

由于您使用的是 XmlTextWriter,并且从您的评论来看,上面的内容显然有点令人困惑,我将添加这些详细信息。

您需要获取当前生成 XML 的代码并将其放置在 App_Code 文件夹中的类中(或者可能放置在 dll 项目中)。 此类将具有一个采用 XmlTextWriter 作为参数的方法。

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

在您的处理程序中,您将拥有以下代码:-

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

请注意,不需要 Response.Write,这可以正确完成编码。

在您的 ASP.NET 页面中,您使用: -

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

yourXmlDataSource.Data = source.ToString();

XmlCreator 可能不需要实例化,在这种情况下您可以使用静态类,它取决于需要哪些其他数据来提供 XM 生成。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文