使用DataTable + ASP.Net处理程序中的数据在浏览器中显示XML

问题描述 投票:0回答:2

我需要在浏览器中显示XML,具有以下标准:

  1. 它应该通过Handler for ASP.Net
  2. 数据直接来自数据库,存储在DataTable中。
  3. 使用此数据表,我需要直接在浏览器中显示XML。

我做的是:

This code is written in ProcessRequest method where is final method is called. GetFeedData is where my SQL query is and returning table. The output for the table is also proper.

或者你可以找到:

private void BuildAYSONationalFeed(HttpContext context, string data)
{
    using (XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
    {
        DataTable dataTable = GetFeedData();

        MemoryStream str = new MemoryStream();
        dataTable.WriteXml(str, true);
        str.Seek(0, SeekOrigin.Begin);
        StreamReader sr = new StreamReader(str);
        string xmlstr;
        xmlstr = sr.ReadToEnd();

        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.Charset = "";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "application/xml";
        context.Response.Write("<?xml version='1.0' encoding='UTF - 8'?>< bookstore >< book >< title > Everyday Italian </ title >< author > Giada De Laurentiis </ author >< year > 2005 </ year >< price > 30.00 </ price ></ book ></ bookstore > ");
        context.Response.Flush();
        context.Response.End();
    }
}

现在,在xmlstr变量中,我得到了这样的XML:I have minimised maximum nodes because of data privacy, but this is what I get in variable.

现在,最终写入响应时显示为纯HTML,没有任何格式。

见这里:Am I missing something? I tried to paste the same XML I get in xmlstr valiable to notepad and loaded it browser. It appears properly. To my best guess, I'm not able to get the proper data to stream.

如果有人可以帮助我,请告诉我。

c# asp.net xml browser httphandler
2个回答
0
投票

我想你正在寻找这个

using (MemoryStream ms = new MemoryStream())
{
    dataTable.WriteXml(ms, true);
    ms.Seek(0, SeekOrigin.Begin);

    using (StreamReader sr = new StreamReader(ms))
    {
        string xmlstr = sr.ReadToEnd();

        //display unaltered xml in multiline textbox (= textarea in html)
        TextBox1.Text = xmlstr;

        //or htmlencode the xml for displaying it in html
        Label1.Text = WebUtility.HtmlEncode(xmlstr);

        //or if you want to display the xml somewhat nicely you have to do some replacing
        Label2.Text = formatXML(xmlstr);
    }
}

用于格式化xml以在html中显示它的辅助方法

string formatXML(string xmlstr)
{
    if (string.IsNullOrEmpty(xmlstr))
    {
        return xmlstr;
    }

    //html encode the xml
    string formattedXml = WebUtility.HtmlEncode(xmlstr);

    //replace the line breaks with html <br>
    formattedXml = formattedXml.Replace(Environment.NewLine, "<br>");

    //indend the xml bij replacing spaces with no break space
    formattedXml = formattedXml.Replace("  ", "&nbsp;&nbsp;");

    return formattedXml;
} 

0
投票

我通过填充我的DataTableDataSet得到了解决方案,并使用了来自GetXML()的内置函数DataSet

请参阅下面的代码:

private void BuildAYSONationalFeed(HttpContext context, DataTable feedDataTable)
    {
        DataSet dataSet = new DataSet("Portals");
        dataSet.Tables.Add(feedDataTable);

        context.Response.Clear();
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.ContentType = "text/xml";
        context.Response.Write(dataSet.GetXml());
        context.Response.Flush();
        context.Response.End();
    }

这完成了这项工作。

因此,GetXML()方法基本上将在DataSet中以表格格式存储的数据加载到XML。我只是向浏览器显示了XML响应。

© www.soinside.com 2019 - 2024. All rights reserved.