以前一直都在学习java和php,今天有人突然加我QQ问我关于团购网站api的问题,而她用的是.net开发的网站,虽然说思路差不多,但是我毕竟美学习过.net,但是既然别人找到你了,你也不能让人空手而归吧??
于是乎,我想到了google,看来几篇文章之后写出了下面的代码,经测试,还行~功夫不负有心人啊~
在此也把他分享给大家,可能写的不是很好,但是作为一些.net新手来说,我感觉还是有点自豪嘀~对于新入门的朋友们可以借鉴着学习,假如你是一名老菜鸟的话,还麻烦您指点一下哦~谢谢!!
第一种方法:直接用response输出;
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
test.aspx.cs
"
Inherits
=
"
test
"
%>
<%
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = " text/xml " ;
Response.Write( " <?xml version=\"1.0\" encoding=\"utf-8\" ?> " );
Response.Write( " <item><people> " );
Response.Write( " <name>花落无声</name> " );
Response.Write( " <QQ>237620122</QQ> " );
Response.Write( " </people></item> " );
%>
<%
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = " text/xml " ;
Response.Write( " <?xml version=\"1.0\" encoding=\"utf-8\" ?> " );
Response.Write( " <item><people> " );
Response.Write( " <name>花落无声</name> " );
Response.Write( " <QQ>237620122</QQ> " );
Response.Write( " </people></item> " );
%>
写完之后我都感觉有点傻,不过也能用,谁叫咱是新手呢?不管了……
第二种方法:利用后台输出;
我直接在Page_Load函数里面生成并输出,朋友们可以自己设置
代码如下(test.aspx.cs):
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
public partial class test : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( " <company></company> " );
// 设置版本信息
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration( " 1.0 " , null , null );
xmldecl.Encoding = " utf-8 " ;
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
// 设置根结点
XmlElement newCompany = doc.DocumentElement;
// 创建新的name
XmlElement newName = doc.CreateElement( " name " );
newName.InnerText = " 公司名称 " ;
// 加入父结点
newCompany.AppendChild(newName);
XmlElement newInfo = doc.CreateElement( " info " );
newInfo.InnerText = " 简介 " ;
newCompany.AppendChild(newInfo);
XmlElement newContactinfo = doc.CreateElement( " contactinfo " );
newContactinfo.InnerText = " 网址 " ;
newCompany.AppendChild(newContactinfo);
XmlElement newContactperson = doc.CreateElement( " contactperson " );
newContactperson.InnerText = " 姓名 " ;
newCompany.AppendChild(newContactperson);
XmlElement newContactzip = doc.CreateElement( " contactzip " );
newContactzip.InnerText = " 邮编 " ;
newCompany.AppendChild(newContactzip);
XmlElement newContactadd = doc.CreateElement( " contactadd " );
newContactadd.InnerText = " 地址 " ;
newCompany.AppendChild(newContactadd);
// 工作列表
// 先创建jobs类表
XmlElement newJobs = doc.CreateElement( " jobs " );
newCompany.AppendChild(newJobs);
// DataSet ds = new DataSet();
// if(ds!=null)
// {
// foreach(DataRow dr in ds.Tables[0].Rows)
for ( int i = 0 ; i < 5 ; i ++ )
{
XmlElement newJob = doc.CreateElement( " job " );
newJobs.AppendChild(newJob);
XmlElement newTitle = doc.CreateElement( " title " );
newTitle.InnerText = i.ToString(); // 职位名称
newJob.AppendChild(newTitle);
XmlElement newUrl = doc.CreateElement( " url " );
newUrl.InnerText = " http://www.baidu.com " ; // 网址
newJob.AppendChild(newUrl);
}
// }
// doc.DocumentElement.AppendChild(newCompany);
XmlTextWriter tr = new XmlTextWriter(Server.MapPath( " ~/test.xml " ), System.Text.Encoding.GetEncoding( " utf-8 " ));
doc.WriteContentTo(tr);
tr.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = " text/xml " ;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( " ~/test.xml " ));
xmlDoc.Save(Response.OutputStream);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
public partial class test : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml( " <company></company> " );
// 设置版本信息
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration( " 1.0 " , null , null );
xmldecl.Encoding = " utf-8 " ;
XmlElement root = doc.DocumentElement;
doc.InsertBefore(xmldecl, root);
// 设置根结点
XmlElement newCompany = doc.DocumentElement;
// 创建新的name
XmlElement newName = doc.CreateElement( " name " );
newName.InnerText = " 公司名称 " ;
// 加入父结点
newCompany.AppendChild(newName);
XmlElement newInfo = doc.CreateElement( " info " );
newInfo.InnerText = " 简介 " ;
newCompany.AppendChild(newInfo);
XmlElement newContactinfo = doc.CreateElement( " contactinfo " );
newContactinfo.InnerText = " 网址 " ;
newCompany.AppendChild(newContactinfo);
XmlElement newContactperson = doc.CreateElement( " contactperson " );
newContactperson.InnerText = " 姓名 " ;
newCompany.AppendChild(newContactperson);
XmlElement newContactzip = doc.CreateElement( " contactzip " );
newContactzip.InnerText = " 邮编 " ;
newCompany.AppendChild(newContactzip);
XmlElement newContactadd = doc.CreateElement( " contactadd " );
newContactadd.InnerText = " 地址 " ;
newCompany.AppendChild(newContactadd);
// 工作列表
// 先创建jobs类表
XmlElement newJobs = doc.CreateElement( " jobs " );
newCompany.AppendChild(newJobs);
// DataSet ds = new DataSet();
// if(ds!=null)
// {
// foreach(DataRow dr in ds.Tables[0].Rows)
for ( int i = 0 ; i < 5 ; i ++ )
{
XmlElement newJob = doc.CreateElement( " job " );
newJobs.AppendChild(newJob);
XmlElement newTitle = doc.CreateElement( " title " );
newTitle.InnerText = i.ToString(); // 职位名称
newJob.AppendChild(newTitle);
XmlElement newUrl = doc.CreateElement( " url " );
newUrl.InnerText = " http://www.baidu.com " ; // 网址
newJob.AppendChild(newUrl);
}
// }
// doc.DocumentElement.AppendChild(newCompany);
XmlTextWriter tr = new XmlTextWriter(Server.MapPath( " ~/test.xml " ), System.Text.Encoding.GetEncoding( " utf-8 " ));
doc.WriteContentTo(tr);
tr.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = " text/xml " ;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( " ~/test.xml " ));
xmlDoc.Save(Response.OutputStream);
}
}