4、先将实体类集合序列化为表现为xml格式的string,然后在webservice中反序列化成Collection<>(注意:不可以是IList<>),然后再传递给业务层对象。
[WebMethod]
public string HelloStusByCollection(string sXml)
{
BLL.Class1 cls = new BLL.Class1();
Collection<Student> stuList = cls.DeSerializerCollection<Student>(sXml, typeof(Collection<Student>));//先反序列化为Collection
return cls.GetName(stuList);
}
/**//// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sXml"></param>
/// <param name="type"></param>
/// <returns></returns>
public Collection<T> DeSerializerCollection<T>(string sXml, Type type)
{
XmlReader reader = XmlReader.Create(new StringReader(sXml));
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);
object obj = serializer.Deserialize(reader);
return (Collection<T>)obj;
}
/**//// <summary>
/// 先将实体类集合序列化为string,然后在webservice中反序列化成Collection<>,然后再传递给业务层对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
string str = "";
Student stuInfo1 = new Student();
stuInfo1.StuName = "lxinxuan";
Student stuInfo2 = new Student();
stuInfo2.StuName = "www.cnblogs.com/lxinxuan";
Collection<Student> stuList = new Collection<Student>();
stuList.Add(stuInfo1);
stuList.Add(stuInfo2);
string stuString = this.Serializer<Collection<Student>>(stuList);//先序列化为xml文件格式的string
str = ser.HelloStusByCollection(stuString);
MessageBox.Show(str);
}
/**//// <summary>
/// 实体类集合序列化为字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objToXml"></param>
/// <returns></returns>
public string Serializer<T>(T objToXml)
{
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(objToXml.GetType());
serializer.Serialize(writer, objToXml);
return writer.GetStringBuilder().ToString();
}
