1.步骤
1.1.创建web工程
1.2.创建webService
1.3.编码
2.代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Xml;
using System.Xml.Serialization;
namespace WebApplication16
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public XmlDocument HelloXml() {
Class1 class1 = new Class1();
class1.Name = "zhang shan";
class1.Age = "20";
XmlSerializer xmlSerializer = new XmlSerializer(class1.GetType());
XmlDocument XmlDoc = new XmlDocument();
MemoryStream Stream = new MemoryStream();
xmlSerializer.Serialize(Stream, class1);
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();
XmlDoc.LoadXml(str);
return XmlDoc;
}
}
}
3.运行结果