让方法返回XML对象

•默认以JSON格式返回数据
•使用ScriptMethodAttribute进行标记
–ResponseFormat属性设为Xml
–Response的Content-Type将为text/xml
•可以使用字符串拼接出XML并输出
•可以返回Xml相关类型
–XmlDocument、XmlElement
•返回普通对象时将使用XmlSerializer输出
–可以使用.NET中强大的XML序列化功能


aspx
    <form id="form1" runat="server">
        
<asp:ScriptManager runat="server" ID="ScriptManager1" ScriptMode="Debug">
            
<Services>
                
<asp:ServiceReference Path="Services/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>

ReturnXmlService.asmx
<%@ WebService Language="C#" Class="ReturnXmlService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Xml;

[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ReturnXmlService  : System.Web.Services.WebService
{    
    [WebMethod]
    [ScriptMethod(ResponseFormat 
= ResponseFormat.Xml)]
    
public XmlNode GetXmlDocument()
    {
        XmlDocument doc 
= new XmlDocument();
        doc.LoadXml(
"<Employee><Name>Jeffrey Zhao</Name><Salary>1000</Salary></Employee>");

        
return doc;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat 
= ResponseFormat.Xml)]
    
public XmlNode GetXmlElement()
    {
        XmlDocument doc 
= new XmlDocument();
        doc.LoadXml(
"<Employee><Name>Jeffrey Zhao</Name><Salary>1000</Salary></Employee>");

        
return doc.DocumentElement;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat 
= ResponseFormat.Xml)]
    
public Employee GetEmployee()
    {
        
return new Employee("Jeffrey Zhao"1000);
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat 
= ResponseFormat.Xml)]
    
public string GetXmlString()
    {
        
return "<Employee><Name>Jeffrey Zhao</Name><Salary>1000</Salary></Employee>";
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat 
= ResponseFormat.Xml, XmlSerializeString = true)]
    
public string GetSerializedString()
    {
        
return "<Employee><Name>Jeffrey Zhao</Name><Salary>1000</Salary></Employee>";
    }
    
}
posted on 2008-04-30 23:45  一粒沙  阅读(637)  评论(0编辑  收藏  举报