Click here to Skip to main content
15,886,806 members
Articles / Programming Languages / XML

.NET XML and SOAP Serialization Samples, Tips

Rate me:
Please Sign up or sign in to vote.
4.82/5 (42 votes)
22 Jul 200510 min read 267.9K   4.7K   110  
Provides samples for XML and SOAP serialization using C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SerializationSamples.SOAP;
using SerializationSamples;
namespace Test
{
	/// <summary>
	/// Summary description for SoapForm.
	/// </summary>
	public class SoapForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txtFilePath;
		private SerializationSamples.SOAP.Exam exam;
		private System.Windows.Forms.Button btFromSoapFile;
		private System.Windows.Forms.TextBox txtSerializedObject;
		private System.Windows.Forms.Button btnToString;
		private System.Windows.Forms.Button btnFromString;
		private System.Windows.Forms.Button btnToSoapFile2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SoapForm(SerializationSamples.SOAP.Exam exam)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.exam=exam;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.txtFilePath = new System.Windows.Forms.TextBox();
			this.btFromSoapFile = new System.Windows.Forms.Button();
			this.btnToString = new System.Windows.Forms.Button();
			this.btnFromString = new System.Windows.Forms.Button();
			this.txtSerializedObject = new System.Windows.Forms.TextBox();
			this.btnToSoapFile2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtFilePath
			// 
			this.txtFilePath.Location = new System.Drawing.Point(152, 16);
			this.txtFilePath.Name = "txtFilePath";
			this.txtFilePath.Size = new System.Drawing.Size(240, 20);
			this.txtFilePath.TabIndex = 1;
			this.txtFilePath.Text = "";
			// 
			// btFromSoapFile
			// 
			this.btFromSoapFile.Location = new System.Drawing.Point(8, 48);
			this.btFromSoapFile.Name = "btFromSoapFile";
			this.btFromSoapFile.Size = new System.Drawing.Size(136, 23);
			this.btFromSoapFile.TabIndex = 2;
			this.btFromSoapFile.Text = "Deserialize from File";
			this.btFromSoapFile.Click += new System.EventHandler(this.btFromSoapFile_Click);
			// 
			// btnToString
			// 
			this.btnToString.Location = new System.Drawing.Point(8, 120);
			this.btnToString.Name = "btnToString";
			this.btnToString.Size = new System.Drawing.Size(136, 23);
			this.btnToString.TabIndex = 3;
			this.btnToString.Text = "Serialize To String";
			this.btnToString.Click += new System.EventHandler(this.btnToString_Click);
			// 
			// btnFromString
			// 
			this.btnFromString.Location = new System.Drawing.Point(8, 152);
			this.btnFromString.Name = "btnFromString";
			this.btnFromString.Size = new System.Drawing.Size(136, 23);
			this.btnFromString.TabIndex = 4;
			this.btnFromString.Text = "Deserialize From String";
			this.btnFromString.Click += new System.EventHandler(this.btnFromString_Click);
			// 
			// txtSerializedObject
			// 
			this.txtSerializedObject.Location = new System.Drawing.Point(152, 88);
			this.txtSerializedObject.Multiline = true;
			this.txtSerializedObject.Name = "txtSerializedObject";
			this.txtSerializedObject.Size = new System.Drawing.Size(240, 160);
			this.txtSerializedObject.TabIndex = 5;
			this.txtSerializedObject.Text = "";
			// 
			// btnToSoapFile2
			// 
			this.btnToSoapFile2.Location = new System.Drawing.Point(8, 16);
			this.btnToSoapFile2.Name = "btnToSoapFile2";
			this.btnToSoapFile2.Size = new System.Drawing.Size(136, 23);
			this.btnToSoapFile2.TabIndex = 6;
			this.btnToSoapFile2.Text = "Serialize To String";
			this.btnToSoapFile2.Click += new System.EventHandler(this.btnToSoapFile_Click);
			// 
			// SoapForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 266);
			this.Controls.Add(this.btnToSoapFile2);
			this.Controls.Add(this.txtSerializedObject);
			this.Controls.Add(this.btnFromString);
			this.Controls.Add(this.btnToString);
			this.Controls.Add(this.btFromSoapFile);
			this.Controls.Add(this.txtFilePath);
			this.Name = "SoapForm";
			this.Text = "Soap Form";
			this.Load += new System.EventHandler(this.SoapForm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnToSoapFile_Click(object sender, System.EventArgs e)
		{
			if (txtFilePath.Text.Length>0)
			{
				XmlUtil.ToSoap(exam,txtFilePath.Text);
				MessageBox.Show("The object is serialized successfully!");
			}
		}

		private void SoapForm_Load(object sender, System.EventArgs e)
		{
			txtFilePath.Text=@"c:\soap.txt";
		}

		private void btFromSoapFile_Click(object sender, System.EventArgs e)
		{
			System.Runtime.Serialization.SerializationBinder binder=new ExamBinder();
			SerializationSamples.SOAP.Exam soapExamFromBinder=XmlUtil.SoapToFromFile(txtFilePath.Text,binder)as SerializationSamples.SOAP.Exam;
			MessageBox.Show("The object is deserialized successfully using the speacial binder!");
			
			SerializationSamples.SOAP.Exam soapExam=XmlUtil.SoapToFromFile(txtFilePath.Text)as SerializationSamples.SOAP.Exam;
			//string author=soapExam.Author;
			MessageBox.Show("The object is deserialized successfully!");

		}	

		private void btnToString_Click(object sender, System.EventArgs e)
		{
			txtSerializedObject.Text=XmlUtil.ToSoap(exam);
			MessageBox.Show("The object is serialized to a string successfully!");
		
		}

		private void btnFromString_Click(object sender, System.EventArgs e)
		{
			if (txtSerializedObject.Text.Length>0){
				SerializationSamples.SOAP.Exam soapExam= XmlUtil.SoapTo(txtSerializedObject.Text) as SerializationSamples.SOAP.Exam;
				MessageBox.Show("The object is deserialized from a string successfully!");
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
United Kingdom United Kingdom

Comments and Discussions