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

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;

    

        
组件设计器生成的代码

        
// 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 窗体设计器生成的代码

        
/// <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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值