[英]How can I save the XML contents of an XDocument as an .xml file?
[英]How can I save PL/SQL output (clob) as an XML file?
在这个阶段我觉得很愚蠢,因为这似乎不是我应该与之斗争的任务,但是经过一点点的睡眠和1000个想法贯穿我的脑海,我想我现在是在隧道中的青蛙 - 视觉病毒使得解决方案蒙上阴影。 开始:
我在PL / SQL中有一个查询,它使用DBMS_XMLGEN.getxml从几个表中选择数据为XML格式(1行,1列是CLOB类型)。 这非常有效。
我的问题在于将此XML输出返回到C#,并询问用户他希望将此输出保存为个人电脑上的output.xml文件。
我正在使用带有JQuery(AJAX)的ASP.net,因此当点击链接并使用返回的数据(来自此实例中的查询的xml)进行保存时,会发布到.aspx页面。 使用带OLEDB datareader的OLEDB命令似乎不是正确的方法......
这样做的最佳方式是什么? 我没有在.net中对序列化进行过多的工作,但我认为这是最好的方法(根据我所做的研究)? 有谁有一个例子说明在我的情况下如何做到这一点?
我的想法基本上是在内存中创建这个输出/文件,询问用户他想要保存它的位置,将其保存为.xml文件并清除内存。
更新:好的我设法创建一个XMLDocument,它可以轻松地接受我的查询中的xml输出。 我怎么能从用户那里得到关于在他的电脑上保存这个文件的位置的输入?
我的代码到目前为止:
Javascript:
function XML_ExportAppRestrictions() {
try {
var data = {"action": "exportAppRestrictions"};
var returned = AjaxLoad('Modules/Common/Common.aspx', null, data, null, null, false);
if (returned.toString().toUpperCase().substring(0, 5) == "ERROR") {
return;
}
else {
// should the file be saved here or in my aspx page using xmldoc.save?
// Using xmldoc.save will only be able to save the file on the server yes?
}
}
catch (ex) {
alert(ex.Message);
}
}
ASP:
if (Request["action"] == "exportAppRestrictions")
{
// Create and open DB connection
conn.ConnectionString = SG_Common.CommonClass.NetRegUDL;
common.OpenDBConnection(ref conn);
// Create command and set SQL statement
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
// select text was removed below due to it's length
cmd.CommandText = "select x as AppRestrictions from dual";
rd = cmd.ExecuteReader();
if (rd.HasRows)
{
if (rd.Read())
{
str = rd["AppRestrictions"].ToString();
}
}
rd.Close();
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.InnerXml = str;
xmldoc.Save("c:\\xmltest.xml");
goto Done;
}
// Done runs in a seperate try/catch statement and only returns data to the calling js function.
Done:
Response.Write(str);
XML应该只是UTF8纯文本。 我已经使用了序列化对象。 也许有人会纠正我,但我没有发现有必要对纯文本或二进制数据使用序列化。
要将这些数据通过内存,这里有一个很好的StackOverflow答案,显示如何使用MemoryStream和字节缓冲区。
因为如果你能得到它变成一个byte []缓冲区,你可以做这样的事情 :
public bool SaveDocument( Byte[] docbinaryarray, string docname)
{
string strdocPath;
strdocPath = "C:\\DocumentDirectory\\" + docname;
FileStream objfilestream =new FileStream(strdocPath,FileMode.Create,FileAccess.ReadWrite);
objfilestream.Write(docbinaryarray,0,docbinaryarray.Length);
objfilestream.Close();
return true;
}
有关System.IO方法的更多信息,请参阅此处 ,例如:
StreamWriter writer = new StreamWriter("c:\\KBTest.txt");
writer.WriteLine("File created using StreamWriter class.");
writer.Close();
this.listbox1.Items.Clear();
addListItem("File Written to C:\\KBTest.txt");
更新1(Windows):
(我以为你在寻找System.Windows.Forms.SaveFileDialog )
以下是此MSDN示例代码:
private void button2_Click(object sender, System.EventArgs e)
{
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch(saveFileDialog1.FilterIndex)
{
case 1 :
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2 :
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3 :
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
}
}
并且,正如它所说,您需要注册事件处理程序:
this.button2.Click += new System.EventHandler(this.button2_Click);
更新2(网站):
好的,我发现你实际上正在寻找一个Web解决方案。 我在想Windows而不是Web。 这是您在Web方面必须做的事情。 这是一个VB.NET示例,但我假设您可以翻译:
string FileName = "Hello.doc";
string Filename = strFileName.Substring(0, FileName.LastIndexOf("."));
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.TransmitFile(Server.MapPath("~/Folder_Name/" + FileName));
Response.End();
我已经多次对这种方法做过变种。 您将XML文件发送回用户的浏览器。 一旦它到达那里,用户必须决定如何处理它(通常,“打开这个文件,或保存它?”)。 这是在浏览者的控制之下。
您可以通过包含一个内容类型标题来帮助浏览器,该标题告诉浏览器它正在查看哪种文件,以及应该尝试打开它的内容。 所以这个标题会要求它调用Excel:
Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
您经常会看到application / pdf,它将提升Adobe Reader或用户正在使用的任何其他PDF阅读器。 在这里,您将要使用内容类型“text / xml”。 这里的内容类型不太重要,因为我们打算让用户保存文件,而不是用应用程序打开它。
内容处理“附件”与“内联”存在一些问题。 尝试两种方式,看看哪一个更适合你。 其中一个(我不记得哪个)基本上忽略了你发送它的建议文件名,而是使用你的ASP页面的名称。 很烦人。 Firefox至少将此作为一个错误记录,IE显然只是认为它是另一个“功能”。
使用Jquery(AJAX)在用户交互时有其局限性(具体参考保存对话框 - 即在我的示例中向asp页面发送请求,并期待文件[或我的情况下的内存流]),并且想要询问用户他想要保存的位置 - 因此不要在服务器上)。
我找到了一个解决方法,因为使用AJAX进行网络相关的调用是我的强项。 这篇文章中提到了一些改进的空间,但对我来说非常合适,因此允许我使用response.write(x)返回我的XML数据,这个“hack”允许用户将XML文件保存在他想要的地方:
Javascript来源:
jQuery.download = function (url, data, method) {
//url and data options required
if (url && data) {
//data can be string of parameters or array/object
data = typeof data == 'string' ? data : jQuery.param(data);
//split params into form inputs
var inputs = '';
jQuery.each(data.split('&'), function () {
var pair = this.split('=');
inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
});
//send request
jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>')
.appendTo('body').submit().remove();
};
};
ASP回报:
Response.Clear();
Response.ContentType = "text/xml";
Response.AddHeader("Content-disposition", "attachment; filename=test.xml");
Response.Write(yourXMLstring);
使用示例(javascript调用):
$.download("your_url_here", html_url_based_parameters_if_needed_here);
在这里得到这个有用的帖子: http : //www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/
希望这有助于将来的某些人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.