使用 DataTable + ASP.Net 处理程序中的数据在浏览器中显示 XML
Display an XML in browser using data from DataTable + ASP.Net handler
我需要在浏览器中显示 XML,条件如下:
- 应该通过Handler for ASP.Net
- 数据直接来自数据库,存储在DataTable中。
- 使用这个数据table,我需要直接在浏览器中显示XML。
我做的是:
或者您可以找到:
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:
现在,当最终写入响应时,显示就像一个没有任何格式的普通 HTLM。
看这里:
如果有人可以提供帮助,请告诉我。
我想你正在找这个
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(" ", " ");
return formattedXml;
}
我通过将 DataTable
填充到 DataSet
并使用 DataSet
中的内置函数 GetXML()
得到了解决方案
请参阅下面的代码段:
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 响应。
我需要在浏览器中显示 XML,条件如下:
- 应该通过Handler for ASP.Net
- 数据直接来自数据库,存储在DataTable中。
- 使用这个数据table,我需要直接在浏览器中显示XML。
我做的是:
或者您可以找到:
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:
现在,当最终写入响应时,显示就像一个没有任何格式的普通 HTLM。
看这里:
如果有人可以提供帮助,请告诉我。
我想你正在找这个
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(" ", " ");
return formattedXml;
}
我通过将 DataTable
填充到 DataSet
并使用 DataSet
GetXML()
得到了解决方案
请参阅下面的代码段:
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 响应。