zoukankan      html  css  js  c++  java
  • 让方法返回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>";
        }
        
    }
  • 相关阅读:
    css属性及属性值
    Typora使用速记(常用的语法和快捷键)
    自己用的RGB对照表
    分享一波银行的面经攒一下人品
    使用hexo+github搭建博客(一)
    使用hexo+github搭建博客(二)配置和宠物系统
    vue3--相对于vue2的改变-T0档次
    java -- md5 加密
    spring boot--注解 案例
    java--整合druid
  • 原文地址:https://www.cnblogs.com/timy/p/1178270.html
Copyright © 2011-2022 走看看