2011-02-01 83 views
0

我在sqlserver中有一個表,在那個表中我有一個有xml文件的列。我在gridview中顯示該表。當我們點擊按鈕時,我想以xml格式顯示特定的xml文件列或導出爲xml文件格式。在ASP.NET頁面上格式化顯示的XML

+0

也許你想編寫一個HttpHandler來生成xml文件並將其返回給用戶,以便他可以下載? – Luke 2011-02-01 17:45:30

回答

0

就可以實現像

dtbl.WriteXml(Server.MapPath("~/yourSample.xml")); 

,也可以是

String xml = dtbl.Rows[0]["XMLColumnName"].ToString(); 
string filename = System.Web.HttpContext.Current.Server.MapPath("~/yourSample.xml"); 
XmlDocument xmlDoc = new XmlDocument(); 
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8); 
xmlWriter.Formatting = Formatting.Indented; 
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"); 
xmlWriter.WriteRaw(xml); 
xmlWriter.Flush(); 
xmlWriter.Close(); 
+0

謝謝你,它工作正常 – 2011-02-01 20:18:29

0

保留原始XML「緩存」(或重新檢索它,如果這對您的應用程序更好)。然後,您只需向新窗口輸出一個可以檢索XML(從緩存或數據庫)並以XML形式顯示給用戶的URL。您需要將響應類型更改爲XML以實現此目的。如果這沒有意義,您需要了解一些有關響應標頭和可能的MIME類型。

0

一個HttpHandler將努力生成xml文件,並返回給用戶,因爲會寫XML與響應正確的內容類型標題例如:

  HttpContext.Current.Response.ClearHeaders(); 
      HttpContext.Current.Response.AppendHeader("Content-Length", dataLength.ToString()); 
     HttpContext.Current.Response.AppendHeader("Content-Transfer-Encoding", "binary"); 
     HttpContext.Current.Response.AppendHeader("Accept-Ranges", "bytes"); 
     if (dataLength > -1) 
      HttpContext.Current.Response.AppendHeader("Accept-Header", dataLength.ToString()); 
     HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("Attachment;Filename=\"{0}\"", fileName)); 
     HttpContext.Current.Response.ContentType = contentType; 

     HttpContext.Current.Response.ClearContent(); 

然後將XML內容寫入響應大塊。