ASP.NET AJAX 客户端 访问 Web Service(6)

本文介绍了如何在WebService中使用不同标记返回XML数据,并通过示例展示了如何创建Employee类、ReturnXmlService.asmx服务及相应的页面实现。客户端通过按钮点击获取不同格式的XML输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

让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>
 

我们比较弹出的效果,就可以看出不同的标记和不同的返回类型,客户端对次不同的处理啦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值