
<%@ WebService Language= "C#" Class= "Sample" %>

using System;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Web.Script.Services;

using System.Collections.Generic;

using System.Text;

using System.Data;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[GenerateScriptType(typeof(Person))]

[ScriptService]

public class Sample : System.Web.Services.WebService

{

[WebMethod]

public string SimpleTypeTest(

string stringTest, bool boolTest, int intTest, float floatTest,

DateTime datetimeTest, DayOfWeek enumTest)

{

return String.Format("stringTest:{0};boolTest:{1};intTest:{2};floatTest:{3};datetimeTest:{4};enumTest:{5}",

stringTest, boolTest, intTest, floatTest, datetimeTest, enumTest);

}

[WebMethod]

public Person GetPerson()

{

return new Person("webabcd", 27);

}

[WebMethod]

public string SetPerson(Person p)

{

return String.Format("Name:{0};Age:{1}", p.Name, p.Age);

}

[WebMethod]

public List<Person> GetList()

{

List<Person> list = new List<Person>();

for (int i = 0; i < 10; i++)

{

Person p = new Person(i.ToString().PadLeft(4, '0'), i + 20);

list.Add(p);

}

return list;

}

[WebMethod]

public string SetList(List<Person> list)

{

StringBuilder sb = new StringBuilder();

sb.Append("<table border='1'>");

foreach (Person p in list)

{

sb.Append("<tr>");

sb.Append("<td>Name:" + p.Name + "</td>");

sb.Append("<td>Age:" + p.Age + "</td>");

sb.Append("</tr>");

}

sb.Append("</table>");

return sb.ToString();

}

[WebMethod]

public Dictionary<string, Person> GetDictionary()

{

Dictionary<string, Person> dictionary = new Dictionary<string, Person>();

for (int i = 0; i < 10; i++)

{

Person p = new Person(i.ToString().PadLeft(4, '0'), i + 20);

dictionary.Add(i.ToString(), p);

}

return dictionary;

}

[WebMethod]

public DataTable GetDataTable()

{

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("Name", typeof(string)));

dt.Columns.Add(new DataColumn("Age", typeof(int)));

for (int i = 0; i < 10; i++)

{

DataRow dr = dt.NewRow();

dr["Name"] = i.ToString().PadLeft(4, '0');

dr["Age"] = i + 20;

dt.Rows.Add(dr);

}

return dt;

}

[WebMethod]

[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]

// 避免ASP.NET AJAX为我们做自动转换,[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)]

public List<Person> GetXml()

{

return GetList();

}

}