比如以下代碼
using System;
using System
using System
using System
namespace Model
{
[Serializable]
public class Student
{
private string stuName;
public Student()
{ }
public string StuName
{
get { return this
set { this
}
}
}
webservice傳遞的內容必須是可序列化的
這種情況下
比如WebService中的方法是
[XmlInclude(typeof(Student))]
[WebMethod]
public string HelloStus(ArrayList stuList)
{
BLL
return cls
} 別漏了[XmlInclude(typeof(Student))]這一行
這個時候
/// <summary>
/// 必須使用webservice中的實體類
/// </summary>
/// <param name=
/// <param name=
private void button
{
string str =
localhost
stuInfo
localhost
stuInfo
IList<localhost
stuList
stuList
object[] array = this
str = ser
MessageBox
}
//這是一個將集合轉換為Objec[]的泛型方法
private object[] ConvertToArray<T>(IList<T> tList)
{
object[] array = new object[tList
int i =
foreach (T t in tList)
{
array[i] = t;
i++;
}
return array;
}
這種情況下
當然
先看webservice中的代碼
[XmlInclude(typeof(Student))]
[WebMethod]
public string HelloStu(Student stuInfo)
{
return stuInfo
} 同樣必須添加這一行代碼[XmlInclude(typeof(Student))]
然後調用代碼是
/**//// <summary>
/// 傳遞單個實體類
/// </summary>
/// <param name=
/// <param name=
private void button
{
string str =
localhost
stuInfo
str = ser
MessageBox
}
這種情況下
同樣先給出webservice中方法的代碼
[WebMethod]
public string HelloStusByList(Collection<Student> stuList)//這裡參數類型是Collection
{
BLL
return cls
} 方法的參數是Collection
表示層調用代碼
/**//// <summary>
/// 傳遞實體類構成的Collection
/// </summary>
/// <param name=
/// <param name=
private void button
{
string str =
localhost
stuInfo
localhost
stuInfo
Collection<localhost
stuList
stuList
str = ser
MessageBox
}
[WebMethod]
public string HelloStusByCollection(string sXml)
{
BLL
Collection<Student> stuList = cls
return cls
}DeserializerCollection方法代碼如下
/**//// <summary>
///
/// </summary>
/// <typeparam name=
/// <param name=
/// <param name=
/// <returns></returns>
public Collection<T> DeSerializerCollection<T>(string sXml
{
XmlReader reader = XmlReader
System
object obj = serializer
return (Collection<T>)obj;
}
表現層調用代碼如下
/**//// <summary>
/// 先將實體類集合序列化為string
/// </summary>
/// <param name=
/// <param name=
private void button
{
string str =
Student stuInfo
stuInfo
Student stuInfo
stuInfo
Collection<Student> stuList = new Collection<Student>();
stuList
stuList
string stuString = this
str = ser
MessageBox
}Serialize方法代碼如下
/**//// <summary>
/// 實體類集合序列化為字符串
/// </summary>
/// <typeparam name=
/// <param name=
/// <returns></returns>
public string Serializer<T>(T objToXml)
{
System
System
serializer
return writer
}
大概就是這些了
From:http://tw.wingwit.com/Article/program/net/201311/13064.html