.net 调用web Service时集合序列化的解决方案
2010-03-12 20:14
369 查看
在.net中调用Web
Service时,如果要返回一个集合,一般使用Dataset或DataTable(Framework 2.0后DataTable也可以序列化了)
DataSet
的优势是可以与XML实现自由转换,可以被.net以外的其他平台调用。
可以参考MSDN:通过 XML Web Services
使用数据集 (ADO.NET) http://msdn.microsoft.com/zh-cn/library/s5xy331f.aspx
遗
憾的是,如果客户端无法传递特定序列化的集合,如自定义的对象集合。
曾经看到萤火虫
这样用:XmlSerializer
序列化实体类来操作xml文件 http://www.cnblogs.com/Marvel/archive/2009/01/20/1379041.html
以
前有做法是转化为DataSet,今天试了下,发现可以不用DataSet也可以实现WebSercice传递序列集合。
新建一个
web service,并建一个三个类:
注意对象集合必须添加属性 [Serializable]
再建一测试方法:
web
Service提供的方法:
服
务OK!
新建一 Winform窗体项目,添加web
service引用,http://localhost/DemoListsByWebService/Service1.asmx
在
主窗体中调用方法:
注意:
DemoWebServiceList.Area[]的集合个数不用Count,MaxItems,而用Length,因为这是一个重新序列化后的数组集
合。
省去了对象转换的烦恼!
邀月注:本文版权由邀月和CSDN共同所有,转载请注明出处。
助人等于自助! 3w@live.cn
Service时,如果要返回一个集合,一般使用Dataset或DataTable(Framework 2.0后DataTable也可以序列化了)
DataSet
的优势是可以与XML实现自由转换,可以被.net以外的其他平台调用。
可以参考MSDN:通过 XML Web Services
使用数据集 (ADO.NET) http://msdn.microsoft.com/zh-cn/library/s5xy331f.aspx
遗
憾的是,如果客户端无法传递特定序列化的集合,如自定义的对象集合。
曾经看到萤火虫
这样用:XmlSerializer
序列化实体类来操作xml文件 http://www.cnblogs.com/Marvel/archive/2009/01/20/1379041.html
以
前有做法是转化为DataSet,今天试了下,发现可以不用DataSet也可以实现WebSercice传递序列集合。
新建一个
web service,并建一个三个类:
using System; using System.Collections.Generic; using System.Collections; using System.Xml; namespace DemoListsByWebService { #region Area public class Area { private string m_Area_ID; /// <summary> /// Area_ID /// </summary> public string Area_ID { get { return m_Area_ID; } set { m_Area_ID = value; } } private string m_Area_Name; /// <summary> /// Area_Name /// </summary> public string Area_Name { get { return m_Area_Name; } set { m_Area_Name = value; } } private double m_Area_Order; /// <summary> /// Area_Order /// </summary> public double Area_Order { get { return m_Area_Order; } set { m_Area_Order = value; } } } #endregion #region AreaCollection [Serializable] public class AreaCollection : CollectionBase { public AreaCollection() { } public AreaCollection(Area[] value) { } public Area this[int index] { get { return ((Area)(this.List[index])); } } public int Add(Area value) { return this.List.Add(value); } public void AddRange(Area[] value) { for (int i = 0; (i < value.Length); i = (i + 1)) { this.Add(value[i]); } } public void AddRange(AreaCollection value) { for (int i = 0; (i < value.Count); i = (i + 1)) { this.Add((Area)value.List[i]); } } public bool Contains(Area value) { return this.List.Contains(value); } public void CopyTo(Area[] array, int index) { this.List.CopyTo(array, index); } public int IndexOf(Area value) { return this.List.IndexOf(value); } public void Insert(int index, Area value) { List.Insert(index, value); } public void Remove(Area value) { List.Remove(value); } private int _maxItems = 0; public int MaxItems { get { return this._maxItems; } set { this._maxItems = value; } } public Area FindByID(string id) { for (int i = 0; (i < Count); i++) { if (this[i].Area_ID == id) { return this[i]; } } return null; } public string FindNameByID(string id) { for (int i = 0; (i < Count); i++) { if (this[i].Area_ID == id) { return this[i].Area_Name; } } return null; } public string FindIDByName(string cateName) { for (int i = 0; (i < Count); i++) { Area tempArea = this[i]; if (tempArea.Area_Name.Trim() == cateName.Trim()) { return tempArea.Area_ID; } } return null; } } [Serializable] public class AreaLists : List<Area> { //Extend your properties and methods by Tony . for more Help to //http://msdn.microsoft.com/zh-cn/library/s6hkc2c4.aspx private int _maxItems = 0; public int MaxItems { get { return this._maxItems; } set { this._maxItems = value; } } } #endregion }
注意对象集合必须添加属性 [Serializable]
再建一测试方法:
using System; using System.Collections.Generic; using System.Web; namespace DemoListsByWebService { public class MyMethods { #region Methods public static AreaLists GetTestArea() { AreaLists al = new AreaLists(); for (int i = 1; i < 11; i++) { Area a = new Area(); a.Area_ID = i.ToString(); a.Area_Name = "第" + i.ToString() + "名"; al.Add(a); } return al; } public static AreaCollection GetTestAreaCollection() { AreaCollection al = new AreaCollection(); for (int i = 1; i < 11; i++) { Area a = new Area(); a.Area_ID = i.ToString(); a.Area_Name = "第" + i.ToString() + "名"; al.Add(a); } return al; } #endregion } }
web
Service提供的方法:
using System; using System.Collections.Generic; using System.Web; using System.Web.Services; namespace DemoListsByWebService { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class Service1 : System.Web.Services.WebService { [WebMethod(Description = "Get Areas")] public AreaLists GetTestArea() { try { AreaLists ac = MyMethods.GetTestArea(); return ac; } catch (Exception e) { return null; } } [WebMethod(Description = "Get AreaCollections")] public AreaCollection GetTestAreaCollection() { try { AreaCollection ac = MyMethods.GetTestAreaCollection(); return ac; } catch (Exception e) { return null; } } } }
服
务OK!
新建一 Winform窗体项目,添加web
service引用,http://localhost/DemoListsByWebService/Service1.asmx
在
主窗体中调用方法:
private void BindAreaList() { DemoWebServiceList.Service1 dl = new TestWebServiceOfLists.DemoWebServiceList.Service1(); DemoWebServiceList.Area[] ac = dl.GetTestArea(); cbArea.DataSource = ac; cbArea.DisplayMember = "Area_Name"; cbArea.ValueMember = "Area_ID"; cbArea.SelectedValue = "5"; lbResult.Text = "集合共:" + ac.Length.ToString() + "个元素!"; } private void BindAreaCollection() { DemoWebServiceList.Service1 dl = new TestWebServiceOfLists.DemoWebServiceList.Service1(); DemoWebServiceList.Area[] ac = dl.GetTestAreaCollection(); cbArea.DataSource = ac; cbArea.DisplayMember = "Area_Name"; cbArea.ValueMember = "Area_ID"; cbArea.SelectedValue = "5"; lbResult.Text = "集合共:" + ac.Length.ToString() + "个元素!"; }
注意:
DemoWebServiceList.Area[]的集合个数不用Count,MaxItems,而用Length,因为这是一个重新序列化后的数组集
合。
省去了对象转换的烦恼!
邀月注:本文版权由邀月和CSDN共同所有,转载请注明出处。
助人等于自助! 3w@live.cn
相关文章推荐
- .net调用web Service时集合序列化的解决方案
- 在.net 2.0环境下开发的web service ,支持http get方式的调用
- 用.Net调用了Java写的的Web Service
- .net序列化与反序列化——提供多次存储对象集后读取不完全解决方案
- Java调用.net Web Service时调用不到方法的时候解决办法
- .NET应用程序中异步调用Web Service的几种方法 come from: veryhappy(wx.net)
- 摘:关于php调用.net的web service 踩过的坑
- Java调用.NET XML Web Services返回的数据集合的方法
- 关于在ASP.NET应用程序中异步调用Web Service的解决方案
- PHP调用.net的webservice 中文乱码解决方案
- java与.net的web service相互调用
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- PHP调用.net的webservice 中文乱码解决方案
- .net序列化及反序列化集合
- .NET 调用SAP Web Service(转)
- 非.NET语言调用.NET XML Web Services返回的数据集合的两个方法
- php调用.Net WebService消息序列化及性能测试
- .NET操作XML文件之泛型集合的序列化与反序列化
- MySQL详解各种乱码错误,java调用mysql中文参数查不到结果等解决方案(大章集合)
- Java调用.Net的web service的几种方式