asp.net网站如何返回xml文档 [英] How an asp.net website return an xml document
本文介绍了asp.net网站如何返回xml文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我的项目必须像这样: http://xml.alexa. com/data?cli = 10& dat = nsa& url = gomm.com.vn [
Hi all,
My project have to act like that: http://xml.alexa.com/data?cli=10&dat=nsa&url=gomm.com.vn[^]
The link with querystring return an xml content.
How can I do that?
Anyone can give me some guideline, please, I''ve googled but not found anything.
Thanks in advance,
TuanNM
推荐答案
设置Response.ContentType ="application/xml"并将xml写入输出流.
Set Response.ContentType = "application/xml" and write your xml to the output stream.
生成xml,将其写入响应输出流,并将响应内容类型设置为"text/xml"或"application/xml".
Generate the xml, write it to the response output stream and set the response content type to "text/xml" or "application/xml".
我已经使用下面提到的代码实现了相同的解决方案.
在页面加载下,编写以下代码.
Hi,
I have achieved the same solution using the code mentioned below.
under page load write below code.
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/xml";
Response.Charset = "utf-8";
GenerateXML("XML_Output", Response.OutputStream);
Response.End();
}
并为GenerateXML方法编写以下代码
and for GenerateXML method write below code
public void GenerateXML(string Format, Stream stream)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("root");
//Get values from the database into a datatable
foreach (DataRow dr in extracteddatatable.Rows)
{
// Repeat this code:
string strDes = "<![CDATA[Class : <b>"+ "]]>";
writer.WriteStartElement("rootelement");
writer.WriteElementString("RLS ", dr["extractedfield"].ToString());
//end location subroot
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
并且不要忘记添加以下提到的名称空间:)
and don''t forget to add below mentioned namespaces :)
using System.Xml.Xsl;
using System.Xml.Schema;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
请让我知道是否有任何问题
please let me know if any problem
这篇关于asp.net网站如何返回xml文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文