zoukankan      html  css  js  c++  java
  • 简单的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的复杂度参看具体项目.但是底层实现基本都差不多的^^.

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

    本文来自博客园,作者:Slashout,转载请注明原文链接:https://www.cnblogs.com/SlashOut/archive/2005/04/05/132423.html 关注公众号:数字化转型

  • 相关阅读:
    用WinForm写的员工考勤项目!!!!!!
    洛谷P1892《[BOI2003]团伙》
    洛谷P1821《[USACO07FEB]银牛派对Silver Cow Party》
    洛谷P1149《火柴棒等式》
    2017 国庆清北刷题冲刺班《角谷猜想》
    洛谷P2330《[SCOI2005]繁忙的都市》
    洛谷P1955《[NOI2015]程序自动分析》
    洛谷P1536《村村通》
    Windows 10 体验记
    洛谷P1102《A-B数对》
  • 原文地址:https://www.cnblogs.com/SlashOut/p/132423.html
Copyright © 2011-2022 走看看