Click here to Skip to main content
15,896,557 members
Articles / Web Development / HTML

XML Data Files, XML Serialization, and .NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (28 votes)
27 Aug 200317 min read 340.5K   6.4K   130  
Describes a means to build XML data files using XML Schema and xsd.exe to facilitate easy XML Serialization
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace CardfileSerializationDemo
{
	/// <summary>
	/// Summary description for frmContact.
	/// </summary>
	public class frmContact : frmCardEdit
	{
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Panel pnlContact;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.TextBox tbCompany;
		private System.Windows.Forms.Label label23;
		private System.Windows.Forms.TextBox tbNote;
		private System.Windows.Forms.Label label22;
		private System.Windows.Forms.TextBox tbEMail;
		private System.Windows.Forms.Label label21;
		private System.Windows.Forms.TextBox tbFAXPh;
		private System.Windows.Forms.Label label20;
		private System.Windows.Forms.TextBox tbWorkPh;
		private System.Windows.Forms.Label label19;
		private System.Windows.Forms.TextBox tbMobilePh;
		private System.Windows.Forms.Label label18;
		private System.Windows.Forms.TextBox tbHomePh;
		private System.Windows.Forms.Label label17;
		private System.Windows.Forms.TextBox tbCountry;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.TextBox tbPCode;
		private System.Windows.Forms.Label label15;
		private System.Windows.Forms.TextBox tbState;
		private System.Windows.Forms.Label label14;
		private System.Windows.Forms.TextBox tbCity;
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.TextBox tbAddr3;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.TextBox tbAddr2;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox tbAddr1;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.TextBox tbLName;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.TextBox tbMName;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.TextBox tbFName;
		private System.Windows.Forms.Label label7;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmContact()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.label2 = new System.Windows.Forms.Label();
			this.pnlContact = new System.Windows.Forms.Panel();
			this.panel1 = new System.Windows.Forms.Panel();
			this.tbCompany = new System.Windows.Forms.TextBox();
			this.label23 = new System.Windows.Forms.Label();
			this.tbNote = new System.Windows.Forms.TextBox();
			this.label22 = new System.Windows.Forms.Label();
			this.tbEMail = new System.Windows.Forms.TextBox();
			this.label21 = new System.Windows.Forms.Label();
			this.tbFAXPh = new System.Windows.Forms.TextBox();
			this.label20 = new System.Windows.Forms.Label();
			this.tbWorkPh = new System.Windows.Forms.TextBox();
			this.label19 = new System.Windows.Forms.Label();
			this.tbMobilePh = new System.Windows.Forms.TextBox();
			this.label18 = new System.Windows.Forms.Label();
			this.tbHomePh = new System.Windows.Forms.TextBox();
			this.label17 = new System.Windows.Forms.Label();
			this.tbCountry = new System.Windows.Forms.TextBox();
			this.label16 = new System.Windows.Forms.Label();
			this.tbPCode = new System.Windows.Forms.TextBox();
			this.label15 = new System.Windows.Forms.Label();
			this.tbState = new System.Windows.Forms.TextBox();
			this.label14 = new System.Windows.Forms.Label();
			this.tbCity = new System.Windows.Forms.TextBox();
			this.label13 = new System.Windows.Forms.Label();
			this.tbAddr3 = new System.Windows.Forms.TextBox();
			this.label12 = new System.Windows.Forms.Label();
			this.tbAddr2 = new System.Windows.Forms.TextBox();
			this.label11 = new System.Windows.Forms.Label();
			this.tbAddr1 = new System.Windows.Forms.TextBox();
			this.label10 = new System.Windows.Forms.Label();
			this.tbLName = new System.Windows.Forms.TextBox();
			this.label9 = new System.Windows.Forms.Label();
			this.tbMName = new System.Windows.Forms.TextBox();
			this.label8 = new System.Windows.Forms.Label();
			this.tbFName = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.pnlContact.SuspendLayout();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// cmdCancel
			// 
			this.cmdCancel.Location = new System.Drawing.Point(496, 264);
			this.cmdCancel.Visible = true;
			// 
			// cmdOK
			// 
			this.cmdOK.Location = new System.Drawing.Point(576, 264);
			this.cmdOK.Visible = true;
			// 
			// tbCardName
			// 
			this.tbCardName.Size = new System.Drawing.Size(568, 20);
			this.tbCardName.Visible = true;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 32);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(128, 16);
			this.label2.TabIndex = 14;
			this.label2.Text = "Contact information:";
			// 
			// pnlContact
			// 
			this.pnlContact.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.pnlContact.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.pnlContact.Controls.AddRange(new System.Windows.Forms.Control[] {
																					 this.panel1});
			this.pnlContact.Location = new System.Drawing.Point(8, 48);
			this.pnlContact.Name = "pnlContact";
			this.pnlContact.Size = new System.Drawing.Size(640, 208);
			this.pnlContact.TabIndex = 15;
			// 
			// panel1
			// 
			this.panel1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.panel1.BackColor = System.Drawing.Color.Ivory;
			this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																				 this.tbCompany,
																				 this.label23,
																				 this.tbNote,
																				 this.label22,
																				 this.tbEMail,
																				 this.label21,
																				 this.tbFAXPh,
																				 this.label20,
																				 this.tbWorkPh,
																				 this.label19,
																				 this.tbMobilePh,
																				 this.label18,
																				 this.tbHomePh,
																				 this.label17,
																				 this.tbCountry,
																				 this.label16,
																				 this.tbPCode,
																				 this.label15,
																				 this.tbState,
																				 this.label14,
																				 this.tbCity,
																				 this.label13,
																				 this.tbAddr3,
																				 this.label12,
																				 this.tbAddr2,
																				 this.label11,
																				 this.tbAddr1,
																				 this.label10,
																				 this.tbLName,
																				 this.label9,
																				 this.tbMName,
																				 this.label8,
																				 this.tbFName,
																				 this.label7});
			this.panel1.Location = new System.Drawing.Point(8, 8);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(624, 192);
			this.panel1.TabIndex = 9;
			// 
			// tbCompany
			// 
			this.tbCompany.BackColor = System.Drawing.Color.Ivory;
			this.tbCompany.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbCompany.Location = new System.Drawing.Point(504, 128);
			this.tbCompany.Name = "tbCompany";
			this.tbCompany.Size = new System.Drawing.Size(112, 20);
			this.tbCompany.TabIndex = 33;
			this.tbCompany.Text = "";
			// 
			// label23
			// 
			this.label23.Location = new System.Drawing.Point(424, 128);
			this.label23.Name = "label23";
			this.label23.Size = new System.Drawing.Size(72, 16);
			this.label23.TabIndex = 32;
			this.label23.Text = "Company:";
			// 
			// tbNote
			// 
			this.tbNote.BackColor = System.Drawing.Color.Ivory;
			this.tbNote.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbNote.Location = new System.Drawing.Point(8, 96);
			this.tbNote.Multiline = true;
			this.tbNote.Name = "tbNote";
			this.tbNote.Size = new System.Drawing.Size(192, 80);
			this.tbNote.TabIndex = 31;
			this.tbNote.Text = "";
			// 
			// label22
			// 
			this.label22.Location = new System.Drawing.Point(8, 80);
			this.label22.Name = "label22";
			this.label22.Size = new System.Drawing.Size(72, 16);
			this.label22.TabIndex = 30;
			this.label22.Text = "Notes:";
			// 
			// tbEMail
			// 
			this.tbEMail.BackColor = System.Drawing.Color.Ivory;
			this.tbEMail.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbEMail.Location = new System.Drawing.Point(504, 104);
			this.tbEMail.Name = "tbEMail";
			this.tbEMail.Size = new System.Drawing.Size(112, 20);
			this.tbEMail.TabIndex = 29;
			this.tbEMail.Text = "";
			// 
			// label21
			// 
			this.label21.Location = new System.Drawing.Point(424, 104);
			this.label21.Name = "label21";
			this.label21.Size = new System.Drawing.Size(72, 16);
			this.label21.TabIndex = 28;
			this.label21.Text = "E-Mail:";
			// 
			// tbFAXPh
			// 
			this.tbFAXPh.BackColor = System.Drawing.Color.Ivory;
			this.tbFAXPh.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbFAXPh.Location = new System.Drawing.Point(504, 80);
			this.tbFAXPh.Name = "tbFAXPh";
			this.tbFAXPh.Size = new System.Drawing.Size(112, 20);
			this.tbFAXPh.TabIndex = 27;
			this.tbFAXPh.Text = "";
			// 
			// label20
			// 
			this.label20.Location = new System.Drawing.Point(424, 80);
			this.label20.Name = "label20";
			this.label20.Size = new System.Drawing.Size(72, 16);
			this.label20.TabIndex = 26;
			this.label20.Text = "FAX ph:";
			// 
			// tbWorkPh
			// 
			this.tbWorkPh.BackColor = System.Drawing.Color.Ivory;
			this.tbWorkPh.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbWorkPh.Location = new System.Drawing.Point(504, 56);
			this.tbWorkPh.Name = "tbWorkPh";
			this.tbWorkPh.Size = new System.Drawing.Size(112, 20);
			this.tbWorkPh.TabIndex = 25;
			this.tbWorkPh.Text = "";
			// 
			// label19
			// 
			this.label19.Location = new System.Drawing.Point(424, 56);
			this.label19.Name = "label19";
			this.label19.Size = new System.Drawing.Size(72, 16);
			this.label19.TabIndex = 24;
			this.label19.Text = "Work ph:";
			// 
			// tbMobilePh
			// 
			this.tbMobilePh.BackColor = System.Drawing.Color.Ivory;
			this.tbMobilePh.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbMobilePh.Location = new System.Drawing.Point(504, 32);
			this.tbMobilePh.Name = "tbMobilePh";
			this.tbMobilePh.Size = new System.Drawing.Size(112, 20);
			this.tbMobilePh.TabIndex = 23;
			this.tbMobilePh.Text = "";
			// 
			// label18
			// 
			this.label18.Location = new System.Drawing.Point(424, 32);
			this.label18.Name = "label18";
			this.label18.Size = new System.Drawing.Size(72, 16);
			this.label18.TabIndex = 22;
			this.label18.Text = "Mobile ph:";
			// 
			// tbHomePh
			// 
			this.tbHomePh.BackColor = System.Drawing.Color.Ivory;
			this.tbHomePh.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbHomePh.Location = new System.Drawing.Point(504, 8);
			this.tbHomePh.Name = "tbHomePh";
			this.tbHomePh.Size = new System.Drawing.Size(112, 20);
			this.tbHomePh.TabIndex = 21;
			this.tbHomePh.Text = "";
			// 
			// label17
			// 
			this.label17.Location = new System.Drawing.Point(424, 8);
			this.label17.Name = "label17";
			this.label17.Size = new System.Drawing.Size(72, 16);
			this.label17.TabIndex = 20;
			this.label17.Text = "Home ph:";
			// 
			// tbCountry
			// 
			this.tbCountry.BackColor = System.Drawing.Color.Ivory;
			this.tbCountry.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbCountry.Location = new System.Drawing.Point(296, 152);
			this.tbCountry.Name = "tbCountry";
			this.tbCountry.Size = new System.Drawing.Size(112, 20);
			this.tbCountry.TabIndex = 19;
			this.tbCountry.Text = "";
			// 
			// label16
			// 
			this.label16.Location = new System.Drawing.Point(216, 152);
			this.label16.Name = "label16";
			this.label16.Size = new System.Drawing.Size(72, 16);
			this.label16.TabIndex = 18;
			this.label16.Text = "Country:";
			// 
			// tbPCode
			// 
			this.tbPCode.BackColor = System.Drawing.Color.Ivory;
			this.tbPCode.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbPCode.Location = new System.Drawing.Point(296, 128);
			this.tbPCode.Name = "tbPCode";
			this.tbPCode.Size = new System.Drawing.Size(112, 20);
			this.tbPCode.TabIndex = 17;
			this.tbPCode.Text = "";
			// 
			// label15
			// 
			this.label15.Location = new System.Drawing.Point(216, 128);
			this.label15.Name = "label15";
			this.label15.Size = new System.Drawing.Size(72, 16);
			this.label15.TabIndex = 16;
			this.label15.Text = "Postal code:";
			// 
			// tbState
			// 
			this.tbState.BackColor = System.Drawing.Color.Ivory;
			this.tbState.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbState.Location = new System.Drawing.Point(296, 104);
			this.tbState.Name = "tbState";
			this.tbState.Size = new System.Drawing.Size(112, 20);
			this.tbState.TabIndex = 15;
			this.tbState.Text = "";
			// 
			// label14
			// 
			this.label14.Location = new System.Drawing.Point(216, 104);
			this.label14.Name = "label14";
			this.label14.Size = new System.Drawing.Size(72, 16);
			this.label14.TabIndex = 14;
			this.label14.Text = "State:";
			// 
			// tbCity
			// 
			this.tbCity.BackColor = System.Drawing.Color.Ivory;
			this.tbCity.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbCity.Location = new System.Drawing.Point(296, 80);
			this.tbCity.Name = "tbCity";
			this.tbCity.Size = new System.Drawing.Size(112, 20);
			this.tbCity.TabIndex = 13;
			this.tbCity.Text = "";
			// 
			// label13
			// 
			this.label13.Location = new System.Drawing.Point(216, 80);
			this.label13.Name = "label13";
			this.label13.Size = new System.Drawing.Size(72, 16);
			this.label13.TabIndex = 12;
			this.label13.Text = "City:";
			// 
			// tbAddr3
			// 
			this.tbAddr3.BackColor = System.Drawing.Color.Ivory;
			this.tbAddr3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbAddr3.Location = new System.Drawing.Point(296, 56);
			this.tbAddr3.Name = "tbAddr3";
			this.tbAddr3.Size = new System.Drawing.Size(112, 20);
			this.tbAddr3.TabIndex = 11;
			this.tbAddr3.Text = "";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(216, 56);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(72, 16);
			this.label12.TabIndex = 10;
			this.label12.Text = "Address 3:";
			// 
			// tbAddr2
			// 
			this.tbAddr2.BackColor = System.Drawing.Color.Ivory;
			this.tbAddr2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbAddr2.Location = new System.Drawing.Point(296, 32);
			this.tbAddr2.Name = "tbAddr2";
			this.tbAddr2.Size = new System.Drawing.Size(112, 20);
			this.tbAddr2.TabIndex = 9;
			this.tbAddr2.Text = "";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(216, 32);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(72, 16);
			this.label11.TabIndex = 8;
			this.label11.Text = "Address 2:";
			// 
			// tbAddr1
			// 
			this.tbAddr1.BackColor = System.Drawing.Color.Ivory;
			this.tbAddr1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbAddr1.Location = new System.Drawing.Point(296, 8);
			this.tbAddr1.Name = "tbAddr1";
			this.tbAddr1.Size = new System.Drawing.Size(112, 20);
			this.tbAddr1.TabIndex = 7;
			this.tbAddr1.Text = "";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(216, 8);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 16);
			this.label10.TabIndex = 6;
			this.label10.Text = "Address 1:";
			// 
			// tbLName
			// 
			this.tbLName.BackColor = System.Drawing.Color.Ivory;
			this.tbLName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbLName.Location = new System.Drawing.Point(88, 56);
			this.tbLName.Name = "tbLName";
			this.tbLName.Size = new System.Drawing.Size(112, 20);
			this.tbLName.TabIndex = 5;
			this.tbLName.Text = "";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(8, 56);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(72, 16);
			this.label9.TabIndex = 4;
			this.label9.Text = "Last name:";
			// 
			// tbMName
			// 
			this.tbMName.BackColor = System.Drawing.Color.Ivory;
			this.tbMName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbMName.Location = new System.Drawing.Point(88, 32);
			this.tbMName.Name = "tbMName";
			this.tbMName.Size = new System.Drawing.Size(112, 20);
			this.tbMName.TabIndex = 3;
			this.tbMName.Text = "";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(8, 32);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(80, 16);
			this.label8.TabIndex = 2;
			this.label8.Text = "Middle name:";
			// 
			// tbFName
			// 
			this.tbFName.BackColor = System.Drawing.Color.Ivory;
			this.tbFName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.tbFName.Location = new System.Drawing.Point(88, 8);
			this.tbFName.Name = "tbFName";
			this.tbFName.Size = new System.Drawing.Size(112, 20);
			this.tbFName.TabIndex = 1;
			this.tbFName.Text = "";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(8, 8);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(64, 16);
			this.label7.TabIndex = 0;
			this.label7.Text = "First name:";
			// 
			// frmContact
			// 
			this.AcceptButton = this.cmdOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(656, 293);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.pnlContact,
																		  this.cmdCancel,
																		  this.cmdOK,
																		  this.tbCardName,
																		  this.label2});
			this.Name = "frmContact";
			this.Text = "Contact";
			this.pnlContact.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		public CardTypeBodyContact ContactInfo
		{
			get
			{
				// Extract the data
				CardTypeBodyContact contact = new CardTypeBodyContact();
				contact.FName = tbFName.Text;
				contact.MName = tbMName.Text;
				contact.LName = tbLName.Text;
				contact.Addr1 = tbAddr1.Text;
				contact.Addr2 = tbAddr2.Text;
				contact.Addr3 = tbAddr3.Text;
				contact.City = tbCity.Text;
				contact.State = tbState.Text;
				contact.PCode = tbPCode.Text;
				contact.Country = tbCountry.Text;
				contact.Company = tbCompany.Text;
				contact.HomePh = tbHomePh.Text;
				contact.MobilePh = tbMobilePh.Text;
				contact.WorkPh = tbWorkPh.Text;
				contact.FaxPh = tbFAXPh.Text;
				contact.EMail = tbEMail.Text;
				contact.Notes = tbNote.Text;

				// Return it
				return contact;
			}
			set
			{
				// Initialize the contact panel
				tbFName.Text = value.FName;
				tbMName.Text = value.MName;
				tbLName.Text = value.LName;
				tbAddr1.Text = value.Addr1;
				tbAddr2.Text = value.Addr2;
				tbAddr3.Text = value.Addr3;
				tbCity.Text = value.City;
				tbState.Text = value.State;
				tbPCode.Text = value.PCode;
				tbCountry.Text = value.Country;
				tbCompany.Text = value.Company;
				tbHomePh.Text = value.HomePh;
				tbMobilePh.Text = value.MobilePh;
				tbWorkPh.Text = value.WorkPh;
				tbFAXPh.Text = value.FaxPh;
				tbEMail.Text = value.EMail;
				tbNote.Text = value.Notes;
			}
		}
	}
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions