加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何将PL / SQL输出(clob)保存为XML文件?

发布时间:2020-12-15 21:49:03 所属栏目:百科 来源:网络整理
导读:在这个阶段我觉得很愚蠢,因为这似乎不是我应该与之斗争的任务,但是经过一点点的睡眠和1000个想法贯穿我的脑海,我想我现在是在隧道中的青蛙 – 视觉病毒使得解决方案蒙上阴影.开始: 我在PL / SQL中有一个查询,它使用DBMS_XMLGEN.getxml从几个表中选择数据为X
在这个阶段我觉得很愚蠢,因为这似乎不是我应该与之斗争的任务,但是经过一点点的睡眠和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,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 answer here显示了如何使用MemoryStream和字节缓冲区.

因为如果你可以把它放到byte []缓冲区,你可以做something like this:

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,docbinaryarray.Length);
    objfilestream.Close();
    return true;
}

参见here for some more info on System.IO methods,例如:

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. Here’s what you have to do on the Web side.这是一个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显然只是认为它是另一个“功能”.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读