格式化ASP.NET页面上的显示XML

时间:2011-02-01 17:43:01

标签: c# .net asp.net sql-server xml

我在sqlserver中有一个表,在该表中我有一个包含xml文件的列。我正在gridview中显示该表。当我们点击按钮时,我想以xml格式显示该特定xml文件列或导出为xml文件格式。

3 个答案:

答案 0 :(得分:0)

保持原始XML“缓存”(或重新检索它,如果这对您的应用程序更好)。然后,您只需向新窗口输出一个URL,该URL可以检索XML(从缓存或数据库)并显示为XML给用户。您需要将响应类型更改为XML才能实现此目的。如果这没有意义,您需要了解一下响应头和可能的MIME类型。

答案 1 :(得分: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();

答案 2 :(得分: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内容以块的形式写入Response。