为什么 ASP.net 在我的 ASHX 文件的输出末尾写入空格?

发布于 2024-11-15 10:00:16 字数 1283 浏览 0 评论 0原文

我编写了一个 ASSX 通用处理程序来输出 XML。然而,由于某种原因,ASP.net 在输出的末尾附加了许多空白字符,这破坏了 XML。

我的代码如下所示:

context.Response.ContentType = "text/xml";

        XmlSerializer oSerializer = new XmlSerializer(typeof(ModelXml[]),new XmlRootAttribute("rows"));
        System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
        System.Xml.XmlTextWriter tw = new System.Xml.XmlTextWriter(ms2,new System.Text.UTF8Encoding());
        oSerializer.Serialize(tw,models);
        string s = System.Text.Encoding.UTF8.GetString(ms2.GetBuffer());
        tw.Close();
        ms2.Close();

        context.Response.Write(s.Trim());
        context.Response.End();

当我通过调试器运行此代码时,我发现字符串 s 确实包含不带空格的 XML 数据。但是,当我将 Internet Explorer 指向此文件时,出现以下错误:

The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 

--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://localhost:5791/XXXXX.ashx'. 

当我在记事本中查看页面源代码时,我看到该文件以正确的 XML 开头,但末尾附加了许多空格。如果删除这些空格,XML 文件就可以在我的浏览器和应用程序中正常工作。

为什么 ASP.net 将这些空格附加到我的输出中?我该怎么办?

I've written an ASHX generic handler to output XML. However, for some reason, ASP.net is appending numerous whitespace characters to the end of the output which breaks the XML.

My code looks like this:

context.Response.ContentType = "text/xml";

        XmlSerializer oSerializer = new XmlSerializer(typeof(ModelXml[]),new XmlRootAttribute("rows"));
        System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
        System.Xml.XmlTextWriter tw = new System.Xml.XmlTextWriter(ms2,new System.Text.UTF8Encoding());
        oSerializer.Serialize(tw,models);
        string s = System.Text.Encoding.UTF8.GetString(ms2.GetBuffer());
        tw.Close();
        ms2.Close();

        context.Response.Write(s.Trim());
        context.Response.End();

When I run this code thru the debugger, I see that the string s does indeed contain the XML data with NO WHITESPACE. However, when I point Internet Explorer at this file, I get the following error:

The XML page cannot be displayed 
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later. 

--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://localhost:5791/XXXXX.ashx'. 

When I view the page source in Notepad, I see that the file begins with the correct XML, but there are numerous spaces appended to the end. If I remove these spaces, the XML file works fine with my browser and applications.

Why is ASP.net appending these spaces to my output and what can I do about it?

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

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

发布评论

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

评论(2

自此以后,行同陌路 2024-11-22 10:00:16

从 MS2.GetBuffer() 切换到 MS2.ToArray()。您正在从 MemoryStream 读取缓冲区,该缓冲区是为了提高效率而预先分配的。您只需要使用过的数据,而不是整个缓冲区。

Switch from MS2.GetBuffer() to MS2.ToArray(). You are reading the buffer from the MemoryStream, which is preallocated for efficiency. You want just the used data, not the whole buffer.

横笛休吹塞上声 2024-11-22 10:00:16

您应该直接序列化为 Response.Output,而不是序列化为 MemoryStream
这应该可以解决问题。

Instead of serializing to a MemoryStream, you should serialize directly to Response.Output.
This should solve the issue.

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