在webservice 中使用ArrayList 对象.

本文介绍了如何在WebService中使用ArrayList对象,由于WebService不直接支持ArrayList,因此需要通过添加属性进行序列化处理。示例代码展示了如何创建序列化类,并在WebMethod中操作这个类,以便客户端可以正确接收和映射返回的数据。

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

经常看到有人在网上问,如何使用WebService中使用ArrayList.其实这是因为没有好好看MSDN的原因.

ArrayList 是一个线性列表对象(它还是有点意思的.),不过WebService不支持其,不是标准变量对象,如果要有WebService中使用得加上属性.  [XmlRoot("根元名")]

例:

______________________序列化的类 YouClass.cs

using System;
using System.Xml.Serialization;

namespace SomeNS

{
 [Serializable]
 [XmlRoot("NewYouClass")]     //该属性需要加上 xml根元的序列化
 public class YouSerializabledClass
 {
  private long m_lID;


   public YouSerializabledClass()
  {
  }
  public long lID
  {
   get
   {
    return m_lID;
   }
   set
   {
    m_lID=value;
   }
  }

}

______________________________操作YouClass的类 A.cs

[XmlInclude(typeof(YouSerializabledClass))]
  public YouSerializabledClass YouSerializabledClassOperation(Some Var)
  {
    YouSerializabledClass.lID=Convert.ToInt64(Var);
     return YouSerializabledClass;
  }

_______________________ WebMethod

[WebMethod]
  [XmlInclude(typeof(YouSerializabledClass))]
  public  YouSerializabledClass YouWebMetho(SomeVar Var)
  {
   YouSerializabledClass YouSerializabledClass1=A.YouSerializabledClassOperation(Var);
   return YouSerializabledClass ;
  } 这样完了之后,将在客户端产生序列化类的影射.

_________________________________客户端的Reference.cs

//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

//
// 此源代码是由 Microsoft.VSDesigner 1.1.4322.573 版自动生成。
//
namespace SomeNS.SomeNSReference {
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System;
    using System.Web.Services.Protocols;
    using System.ComponentModel;
    using System.Web.Services;
    
    
  [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/BeginYouWebMetho", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        [return: System.Xml.Serialization.XmlElementAttribute("NewYouClass", IsNullable=true)]
        public YouSerializabledClass YouWebMetho(string EPID) {
            object[] results = this.Invoke("YouWebMetho", new object[] {
                        EPID});
            return ((YouSerializabledClass )(results[0]));
        }
       
        /// <remarks/>
        public System.IAsyncResult BeginYouWebMetho(SomeVar Var, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("BeginYouWebMetho", new object[] {
                        Var}, callback, asyncState);
        }
       
        /// <remarks/>
        public YouSerializabledClass EndGetYouWebMetho(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((YouSerializabledClass )(results[0]));
        }

                     
        /// <remarks/>
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="
http://tempuri.org/")]
    public class YouSerializabledClass
{
       
        /// <remarks/>
        public long lID;
       } 这就是你产生的序列化类.
   
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值