您的位置:首页 > 其它

简单的WindowsForm Client与WebService通信例子(对象的序列化与反序列化)

2005-06-05 23:57 615 查看
1.创建一个WebService,添加一个自定义类:


using System;


using System.Web.Services;


using System.Xml;


using System.Xml.Serialization;


using System.Xml.Schema;


using System.Collections;






namespace WebServiceForStock






{


    public class Person




    

{


        [XmlElement(ElementName="UserName")]


        public string Name;


        [XmlElement(ElementName="UserAge")]


        public int Age;


    }


    public class User




    

{


        public Person personInstance ;


        [XmlElement(ElementName="UserEmail")]


        public string Email;


        [XmlElement(ElementName="UserPassword")]


        public string pwd;




    }






    /**//// <summary>


    /// CustomizeClass 的摘要说明。


    /// 自定义类用XML序列化


    /// 可以返回复合的类


    /// </summary>


    public class CustomizeClass




    

{




        [XmlAttribute()]public int orderID;


        public DateTime orderTime;


        [XmlElement("DateTimeRequired")]public DateTime requiredDate;


        public DateTime shippedDate;


        public ArrayList Details;


        [XmlIgnore]public string SalesPersonID;


        public CustomizeClass()




        

{


            //


            // TODO: 在此处添加构造函数逻辑


            //


        }        


    }


    


    [XmlRoot(Namespace = "http://www.cnblogs.com/slashout/")]


    public class Group




    

{


        public string GroupName;




        // This is for serializing Employee elements.


        [XmlAnyElement(Name = "Employee")]


        public XmlElement[] UnknownEmployees;




        // This is for serializing City elements.   


        [XmlAnyElement


             (Name = "City", 


             Namespace = "http://www.cnblogs.com/slashout/")]


        public XmlElement[] UnknownCity;




        // This one is for all other unknown elements.


        [XmlAnyElement]


        public XmlElement[] UnknownElements;


    }






}



2.在Service.asmx内进行编辑


using System;


using System.Collections;


using System.ComponentModel;


using System.Data;


using System.Diagnostics;


using System.Web;


using System.Web.Services;




namespace WebServiceForStock






{




    /**//// <summary>


    /// Service1 的摘要说明。


    /// </summary>


    public class Service1 : System.Web.Services.WebService




    

{


        public Service1()




        

{


            //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的


            InitializeComponent();


        }




        private System.Windows.Forms.TextBox textBox1;




    






        组件设计器生成的代码#region 组件设计器生成的代码


        


        //Web 服务设计器所必需的


        private IContainer components = null;


                




        /**//// <summary>


        /// 设计器支持所需的方法 - 不要使用代码编辑器修改


        /// 此方法的内容。


        /// </summary>


        private void InitializeComponent()




        

{


            this.textBox1 = new System.Windows.Forms.TextBox();


            // 


            // textBox1


            // 


            this.textBox1.Location = new System.Drawing.Point(17, 17);


            this.textBox1.Name = "textBox1";


            this.textBox1.TabIndex = 0;


            this.textBox1.Text = "textBox1";




        }






        /**//// <summary>


        /// 清理所有正在使用的资源。


        /// </summary>


        protected override void Dispose( bool disposing )




        

{


            if(disposing && components != null)




            

{


                components.Dispose();


            }


            base.Dispose(disposing);        


        }


        


        #endregion




        // WEB 服务示例


        // HelloWorld() 示例服务返回字符串 Hello World


        // 若要生成,请取消注释下列行,然后保存并生成项目


        // 若要测试此 Web 服务,请按 F5 键




        [WebMethod]


        public User StockService(int UserID)




        

{


            User newUser=new User();


            newUser.personInstance=new Person();


            newUser.personInstance.Name="Slashout";


            newUser.personInstance.Age=25;


            newUser.Email="slashout@163.com";


            newUser.pwd="test";


            return newUser;


        }


    }


}



3.添加一个WindowsApp工程,添加Web引用,本人用的引用名称为 EDIService


using System;


using System.Drawing;


using System.Collections;


using System.ComponentModel;


using System.Windows.Forms;


using System.Data;


using System.Web;


using ServiceClient.EDIService;




namespace ServiceClient






{




    /**//// <summary>


    /// Form1 的摘要说明。


    /// </summary>


    public class Form1 : System.Windows.Forms.Form




    

{


        private System.Windows.Forms.Button button1;


        private System.Windows.Forms.Button button2;


        private System.Windows.Forms.TextBox textName;


        private System.Windows.Forms.TextBox textAge;


        private System.Windows.Forms.TextBox textEmail;


        private System.Windows.Forms.TextBox textPassword;




        /**//// <summary>


        /// 必需的设计器变量。


        /// </summary>


        private System.ComponentModel.Container components = null;




        public Form1()




        

{


            //


            // Windows 窗体设计器支持所必需的


            //


            InitializeComponent();




            //


            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码


            //


        }






        /**//// <summary>


        /// 清理所有正在使用的资源。


        /// </summary>


        protected override void Dispose( bool disposing )




        

{


            if( disposing )




            

{


                if (components != null) 




                

{


                    components.Dispose();


                }


            }


            base.Dispose( disposing );


        }






        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码




        /**//// <summary>


        /// 设计器支持所需的方法 - 不要使用代码编辑器修改


        /// 此方法的内容。


        /// </summary>


        private void InitializeComponent()




        

{


            this.button1 = new System.Windows.Forms.Button();


            this.button2 = new System.Windows.Forms.Button();


            this.textName = new System.Windows.Forms.TextBox();


            this.textAge = new System.Windows.Forms.TextBox();


            this.textEmail = new System.Windows.Forms.TextBox();


            this.textPassword = new System.Windows.Forms.TextBox();


            this.SuspendLayout();


            // 


            // button1


            // 


            this.button1.Location = new System.Drawing.Point(72, 200);


            this.button1.Name = "button1";


            this.button1.Size = new System.Drawing.Size(120, 23);


            this.button1.TabIndex = 0;


            this.button1.Text = "GetTheWebService";


            this.button1.Click += new System.EventHandler(this.button1_Click);


            // 


            // button2


            // 


            this.button2.Location = new System.Drawing.Point(272, 200);


            this.button2.Name = "button2";


            this.button2.Size = new System.Drawing.Size(136, 23);


            this.button2.TabIndex = 1;


            this.button2.Text = "RequestTheWebService";


            this.button2.Click += new System.EventHandler(this.button2_Click);


            // 


            // textName


            // 


            this.textName.Location = new System.Drawing.Point(64, 16);


            this.textName.Name = "textName";


            this.textName.Size = new System.Drawing.Size(208, 21);


            this.textName.TabIndex = 2;


            this.textName.Text = "Name";


            // 


            // textAge


            // 


            this.textAge.Location = new System.Drawing.Point(64, 56);


            this.textAge.Name = "textAge";


            this.textAge.Size = new System.Drawing.Size(208, 21);


            this.textAge.TabIndex = 3;


            this.textAge.Text = "Age";


            // 


            // textEmail


            // 


            this.textEmail.Location = new System.Drawing.Point(64, 96);


            this.textEmail.Name = "textEmail";


            this.textEmail.Size = new System.Drawing.Size(208, 21);


            this.textEmail.TabIndex = 4;


            this.textEmail.Text = "Email";


            // 


            // textPassword


            // 


            this.textPassword.Location = new System.Drawing.Point(64, 136);


            this.textPassword.Name = "textPassword";


            this.textPassword.Size = new System.Drawing.Size(208, 21);


            this.textPassword.TabIndex = 5;


            this.textPassword.Text = "PassWord";


            // 


            // Form1


            // 


            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);


            this.ClientSize = new System.Drawing.Size(496, 273);


            this.Controls.Add(this.textPassword);


            this.Controls.Add(this.textEmail);


            this.Controls.Add(this.textAge);


            this.Controls.Add(this.textName);


            this.Controls.Add(this.button2);


            this.Controls.Add(this.button1);


            this.Name = "Form1";


            this.Text = "Form1";


            this.ResumeLayout(false);




        }


        #endregion






        /**//// <summary>


        /// 应用程序的主入口点。


        /// </summary>


        [STAThread]


        static void Main() 




        

{


            Application.Run(new Form1());


        }


        




        /**//// <summary>


        /// 从WebService上反序列化XML


        /// </summary>


        private void UnSerializeSOAP()




        

{                        


            //实例化服务


            ServiceClient.EDIService.Service1 edi=new EDIService.Service1();




            //实例化类


            ServiceClient.EDIService.User newUser=edi.StockService(1);




            //




            this.textName.Text=newUser.personInstance.Name;




            this.textAge.Text=newUser.personInstance.Age.ToString();




            this.textPassword.Text=newUser.pwd;




            this.textEmail.Text=newUser.Email;


        


                                 


        }




        private void button1_Click(object sender, System.EventArgs e)




        

{


            this.UnSerializeSOAP();            


        }




        private void button2_Click(object sender, System.EventArgs e)




        

{


        


        }


    }


}



4.好了,那么我们将WindowsApp设为启动项目,可以运行了^^

5.WebService返回的SOAP消息是这样的:


  <?xml version="1.0" encoding="utf-8" ?> 


- <User xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">


- <personInstance>


  <UserName>Slashout</UserName> 


  <UserAge>25</UserAge> 


  </personInstance>


  <UserEmail>slashout@163.com</UserEmail> 


  <UserPassword>test</UserPassword> 


  </User>

5.这个小代码段非常简单,但是你可以在WebService部分扩展自定义的类,从Web DB上返回你需要串行化的对象实例,其实BizLogic的复杂度参看具体项目.但是底层实现基本都差不多的
源文:http://www.cnblogs.com/slashout/archive/2005/04/05/132423.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息