经常看到有人在网上问,如何使用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;?????? } 这就是你产生的序列化类.???????}