让WebService方法返回XML对象
- 默认以JSON格式返回数据
- 使用ScriptMethodAttribute进行标记(ResponseFormat属性设置为Xml,Response的Context-Type将为text/xml)
- 可以使用字符串拼接出XML并输出
- 可以返回Xml相关类型(XmlDocument,XmlElement)
- 返回普通对象时将使用XmlSerializer输出
一个让方法返回XML对象的示例
首先创建一个Employee类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///Employee 的摘要说明
/// </summary>
public class Employee
{
private string _firstname;
private string _lastname;
private string _title;
public Employee(string firstname,string lastname,string title)
{
this._firstname = firstname;
this._lastname = lastname;
this._title = title;
}
public Employee() { }
public string FirstName
{
get { return this._firstname; }
set { this._firstname = value; }
}
public string LastName
{
set { this._lastname = value; }
get { return this._lastname; }
}
public string Title
{
get { return this._title; }
}
public int Salary { get; set; }
public string FullName
{
get
{
return this.FirstName + this.LastName;
}
}
public static implicit operator string(Employee employee)
{
return employee.FullName;
}
}
然后创建一个ReturnXmlService.asmx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Xml;
/// <summary>
///ReturnXmlService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class ReturnXmlService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlNode GetXmlDocument()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Employee><Name>Xiaoyaojian</Name><Salary>5000</Salary></Employee>");
return doc;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public XmlNode GetXmlElement()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<Employee><Name>Xiaoyaojian</Name><Salary>5000</Salary></Employee>");
return doc.DocumentElement;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public Employee GetEmployee()
{
return new Employee("bai", "yulong","developer");
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public string GetXmlString()
{
return "<Employee><Name>Xiaoyaojian</Name><Salary>5000</Salary></Employee>";
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString = true)]
public string GetSerializedString()
{
return "<Employee><Name>Xiaoyaojian</Name><Salary>5000</Salary></Employee>";
}
}
然后创建页面,使用这个WebService
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Demo03/ReturnXmlService.asmx" InlineScript="true"/>
</Services>
</asp:ScriptManager>
<input type="button" value="GetXmlDocument" onclick="ReturnXmlService.GetXmlDocument(onSucceeded);" /><br /><br />
<input type="button" value="GetXmlElement" onclick="ReturnXmlService.GetXmlElement(onSucceeded);" /><br /><br />
<input type="button" value="GetEmployee" onclick="ReturnXmlService.GetEmployee(onSucceeded);" /><br /><br />
<input type="button" value="GetXmlString" onclick="ReturnXmlService.GetXmlString(onSucceeded);" /><br /><br />
<input type="button" value="GetSerializedString" onclick="ReturnXmlService.GetSerializedString(onSucceeded);" />
<script language="javascript" type="text/javascript">
function onSucceeded(result)
{
alert(result.xml);
}
</script>
</form>
</body>
</html>
我们比较弹出的效果,就可以看出不同的标记和不同的返回类型,客户端对次不同的处理啦