让方法返回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> " ;
    }
    
}

转载于:https://www.cnblogs.com/timy/archive/2008/04/30/1178270.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值