本文介绍了从通过ASP.NET数据集中保存XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有两个code样品,一旦将表从我的数据库,将其放入一个DataSet,然后保存数据集和XML文件在我的桌面。

Currently I have two code samples, once takes a table from my database, puts it into a DataSet and then saves the DataSet and an XML file to my desktop.

另外code保存来自网页的文件。我想这样做是结合这两种code的例子,这样我可以通过互联网保存我的XML文件。

The other code saves a file from a webpage. What I would like to do is combine these two code examples so that I can save my XML file through the internet.

下面是样本:

SqlDataAdapter adapter = new SqlDataAdapter(query, connectionString);
            DataSet questionSet = new DataSet();
            adapter.Fill(questionSet, "Questions");

            questionSet.WriteXml("../../../../../Users/User/Desktop/MyTest");

这成功保存我的表作为XML文档,并将其保存到我的桌面。

This successfully saves my table as an XML document and saves it to my Desktop.

的 code从MSDN 样品通过ASP.NET保存文件

The code sample from the MSDN for saving files through ASP.NET

System.String filename = "myFile.txt";

                   Response.ContentType = "text/xml";

                   // initialize the http content-disposition header to
                   // indicate a file attachment with the default filename
                   // "myFile.txt"
                   System.String disHeader = "Attachment; Filename=\"" + filename +
                      "\"";
                   Response.AppendHeader("Content-Disposition", disHeader);

                   // transfer the file byte-by-byte to the response object
                   System.IO.FileInfo fileToDownload = new
                      System.IO.FileInfo("C:\\downloadJSP\\DownloadConv\\myFile.txt");
                   Response.Flush();
                   Response.WriteFile(fileToDownload.FullName);}

这顺利通过我的浏览器保存的文件。

This successfully saves a file through my browser.

据我的理解,似乎 .WriteXml 创建一个从DataSet中的文件。有没有一种方法扔在另一code该文件,并告诉浏览器下载 .WriteXml 文件呢?

As far as I understand it, it seems that .WriteXml creates a the file from the dataSet. Is there a way to throw that file at the other code and tell the browser to download the .WriteXml file instead?

这一切code的看起来几乎像魔术给我,但话又说回来,这是.NET,这是其仙尘知!

All of this code looks almost like magic to me, but then again, it's .NET, which is known for its pixie dust!

推荐答案

我相信所有你需要的是这样的:

I believe all you need is this:

questionSet.WriteXml(Response.OutputStream);

您还是要设置的内容类型,但你并不需要乱用文件系统来做到这一点。

You still want to set the content type, but you don't need to mess with the file system to do this.

这篇关于从通过ASP.NET数据集中保存XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 19:33