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

该博客展示了一个简单的WindowsForm Client与WebService通信例子,包含对象的序列化与反序列化。定义了Person、User等类,实现了Web服务方法StockService,还展示了在Windows窗体中从WebService反序列化XML数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建一个WebService,添加一个自定义类:

None.gifusing System;
None.gif
using System.Web.Services;
None.gif
using System.Xml;
None.gif
using System.Xml.Serialization;
None.gif
using System.Xml.Schema;
None.gif
using System.Collections;
None.gif
None.gif
None.gif
namespace WebServiceForStock
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class Person
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [XmlElement(ElementName
="UserName")]
InBlock.gif        
public string Name;
InBlock.gif        [XmlElement(ElementName
="UserAge")]
InBlock.gif        
public int Age;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public class User
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Person personInstance ;
InBlock.gif        [XmlElement(ElementName
="UserEmail")]
InBlock.gif        
public string Email;
InBlock.gif        [XmlElement(ElementName
="UserPassword")]
InBlock.gif        
public string pwd;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// CustomizeClass 的摘要说明。
InBlock.gif    
/// 自定义类用XML序列化
InBlock.gif    
/// 可以返回复合的类
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class CustomizeClass
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        [XmlAttribute()]
public int orderID;
InBlock.gif        
public DateTime orderTime;
InBlock.gif        [XmlElement(
"DateTimeRequired")]public DateTime requiredDate;
InBlock.gif        
public DateTime shippedDate;
InBlock.gif        
public ArrayList Details;
InBlock.gif        [XmlIgnore]
public string SalesPersonID;
InBlock.gif        
public CustomizeClass()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }
        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif    [XmlRoot(Namespace 
= "http://www.cnblogs.com/slashout/")]
InBlock.gif    
public class Group
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public string GroupName;
InBlock.gif
InBlock.gif        
// This is for serializing Employee elements.
InBlock.gif
        [XmlAnyElement(Name = "Employee")]
InBlock.gif        
public XmlElement[] UnknownEmployees;
InBlock.gif
InBlock.gif        
// This is for serializing City elements.   
InBlock.gif
        [XmlAnyElement
InBlock.gif             (Name 
= "City"
InBlock.gif             Namespace 
= "http://www.cnblogs.com/slashout/")]
InBlock.gif        
public XmlElement[] UnknownCity;
InBlock.gif
InBlock.gif        
// This one is for all other unknown elements.
InBlock.gif
        [XmlAnyElement]
InBlock.gif        
public XmlElement[] UnknownElements;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif


2.在Service.asmx内进行编辑

None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Data;
None.gif
using System.Diagnostics;
None.gif
using System.Web;
None.gif
using System.Web.Services;
None.gif
None.gif
namespace WebServiceForStock
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Service1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Service1 : System.Web.Services.WebService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public Service1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InBlock.gif
            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private System.Windows.Forms.TextBox textBox1;
InBlock.gif
InBlock.gif    
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码
InBlock.gif        
InBlock.gif        
//Web 服务设计器所必需的
InBlock.gif
        private IContainer components = null;
InBlock.gif                
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.textBox1 = new System.Windows.Forms.TextBox();
InBlock.gif            
// 
InBlock.gif            
// textBox1
InBlock.gif            
// 
InBlock.gif
            this.textBox1.Location = new System.Drawing.Point(1717);
InBlock.gif            
this.textBox1.Name = "textBox1";
InBlock.gif            
this.textBox1.TabIndex = 0;
InBlock.gif            
this.textBox1.Text = "textBox1";
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(disposing && components != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                components.Dispose();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose(disposing);        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
// WEB 服务示例
InBlock.gif        
// HelloWorld() 示例服务返回字符串 Hello World
InBlock.gif        
// 若要生成,请取消注释下列行,然后保存并生成项目
InBlock.gif        
// 若要测试此 Web 服务,请按 F5 键
InBlock.gif

InBlock.gif        [WebMethod]
InBlock.gif        
public User StockService(int UserID)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            User newUser
=new User();
InBlock.gif            newUser.personInstance
=new Person();
InBlock.gif            newUser.personInstance.Name
="Slashout";
InBlock.gif            newUser.personInstance.Age
=25;
InBlock.gif            newUser.Email
="slashout@163.com";
InBlock.gif            newUser.pwd
="test";
InBlock.gif            
return newUser;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


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

None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Windows.Forms;
None.gif
using System.Data;
None.gif
using System.Web;
None.gif
using ServiceClient.EDIService;
None.gif
None.gif
namespace ServiceClient
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Button button1;
InBlock.gif        
private System.Windows.Forms.Button button2;
InBlock.gif        
private System.Windows.Forms.TextBox textName;
InBlock.gif        
private System.Windows.Forms.TextBox textAge;
InBlock.gif        
private System.Windows.Forms.TextBox textEmail;
InBlock.gif        
private System.Windows.Forms.TextBox textPassword;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.button2 = new System.Windows.Forms.Button();
InBlock.gif            
this.textName = new System.Windows.Forms.TextBox();
InBlock.gif            
this.textAge = new System.Windows.Forms.TextBox();
InBlock.gif            
this.textEmail = new System.Windows.Forms.TextBox();
InBlock.gif            
this.textPassword = new System.Windows.Forms.TextBox();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(72200);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.Size = new System.Drawing.Size(12023);
InBlock.gif            
this.button1.TabIndex = 0;
InBlock.gif            
this.button1.Text = "GetTheWebService";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// button2
InBlock.gif            
// 
InBlock.gif
            this.button2.Location = new System.Drawing.Point(272200);
InBlock.gif            
this.button2.Name = "button2";
InBlock.gif            
this.button2.Size = new System.Drawing.Size(13623);
InBlock.gif            
this.button2.TabIndex = 1;
InBlock.gif            
this.button2.Text = "RequestTheWebService";
InBlock.gif            
this.button2.Click += new System.EventHandler(this.button2_Click);
InBlock.gif            
// 
InBlock.gif            
// textName
InBlock.gif            
// 
InBlock.gif
            this.textName.Location = new System.Drawing.Point(6416);
InBlock.gif            
this.textName.Name = "textName";
InBlock.gif            
this.textName.Size = new System.Drawing.Size(20821);
InBlock.gif            
this.textName.TabIndex = 2;
InBlock.gif            
this.textName.Text = "Name";
InBlock.gif            
// 
InBlock.gif            
// textAge
InBlock.gif            
// 
InBlock.gif
            this.textAge.Location = new System.Drawing.Point(6456);
InBlock.gif            
this.textAge.Name = "textAge";
InBlock.gif            
this.textAge.Size = new System.Drawing.Size(20821);
InBlock.gif            
this.textAge.TabIndex = 3;
InBlock.gif            
this.textAge.Text = "Age";
InBlock.gif            
// 
InBlock.gif            
// textEmail
InBlock.gif            
// 
InBlock.gif
            this.textEmail.Location = new System.Drawing.Point(6496);
InBlock.gif            
this.textEmail.Name = "textEmail";
InBlock.gif            
this.textEmail.Size = new System.Drawing.Size(20821);
InBlock.gif            
this.textEmail.TabIndex = 4;
InBlock.gif            
this.textEmail.Text = "Email";
InBlock.gif            
// 
InBlock.gif            
// textPassword
InBlock.gif            
// 
InBlock.gif
            this.textPassword.Location = new System.Drawing.Point(64136);
InBlock.gif            
this.textPassword.Name = "textPassword";
InBlock.gif            
this.textPassword.Size = new System.Drawing.Size(20821);
InBlock.gif            
this.textPassword.TabIndex = 5;
InBlock.gif            
this.textPassword.Text = "PassWord";
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(496273);
InBlock.gif            
this.Controls.Add(this.textPassword);
InBlock.gif            
this.Controls.Add(this.textEmail);
InBlock.gif            
this.Controls.Add(this.textAge);
InBlock.gif            
this.Controls.Add(this.textName);
InBlock.gif            
this.Controls.Add(this.button2);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 从WebService上反序列化XML
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void UnSerializeSOAP()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{                        
InBlock.gif            
//实例化服务
InBlock.gif
            ServiceClient.EDIService.Service1 edi=new EDIService.Service1();
InBlock.gif
InBlock.gif            
//实例化类
InBlock.gif
            ServiceClient.EDIService.User newUser=edi.StockService(1);
InBlock.gif
InBlock.gif            
//
InBlock.gif

InBlock.gif            
this.textName.Text=newUser.personInstance.Name;
InBlock.gif
InBlock.gif            
this.textAge.Text=newUser.personInstance.Age.ToString();
InBlock.gif
InBlock.gif            
this.textPassword.Text=newUser.pwd;
InBlock.gif
InBlock.gif            
this.textEmail.Text=newUser.Email;
InBlock.gif        
InBlock.gif                                 
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.UnSerializeSOAP();            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void button2_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


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

5.WebService返回的SOAP消息是这样的:
None.gif  <?xml version="1.0" encoding="utf-8" ?> 
None.gif
<User xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
None.gif
<personInstance>
None.gif  
<UserName>Slashout</UserName> 
None.gif  
<UserAge>25</UserAge> 
None.gif  
</personInstance>
None.gif  
<UserEmail>slashout@163.com</UserEmail> 
None.gif  
<UserPassword>test</UserPassword> 
None.gif  
</User>


5.这个小代码段非常简单,但是你可以在WebService部分扩展自定义的类,从Web DB上返回你需要串行化的对象实例,其实BizLogic的复杂度参看具体项目.但是底层实现基本都差不多的^^.

总之很简单就很容易看懂了^^.

转载于:https://www.cnblogs.com/SlashOut/archive/2005/04/05/132423.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值