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.Collections;
namespace SerializationSamples.SOAP {
	
	public enum PROPERTYNAME{
		AUTHOR=0
	}
	
	[Serializable]
	public class Exam{
		public Exam(){
			header=new Header();
			questions=new ArrayList();
			properties=new Hashtable();
		}
		private Header header;
		private ArrayList questions;
		private Hashtable properties;
		public Header Header{
			get{return header;}
			set{header=value;}
		}
		public ArrayList Questions{
			get{return questions ;}
			set{questions=value;}
		}
		public string Author{
			get{
				if (properties[PROPERTYNAME.AUTHOR]==null)
				{
					return "";
				}
				else{
					return (string)properties[PROPERTYNAME.AUTHOR];
				}
			}
			set{
				properties[PROPERTYNAME.AUTHOR]=value;
			}
		}
	}

	[Serializable]
	public class Header{
		public Header(){}
		private string title;
		private string description;
		public string Title{
			get{return title;}
			set{title=value;}
		}
		public string Description{
			get{return description;}
			set{description=value;}
		}
	}

	[Serializable]
	public class Question{
		public Question(){}
		private int id;
		private string title;
		private string[] items;
		public int ID{
			get{return id;}
			set{id=value;}
		}
		public string Title{
			get{return title;}
			set{title=value;}
		}
		public string[] Items{
			get{return items;}
			set{items=value;}
		}
	}

	public class ExamBinder:System.Runtime.Serialization.SerializationBinder {
		public override Type BindToType(string assemblyName, string typeName) { 
			string[] typeInfo=typeName.Split('.');
			string className=typeInfo[typeInfo.Length -1];//The latest item is the class name
			if (className.Equals("Exam")){
				return typeof (Exam);
			}
			else if (className.Equals("Header")){
				return typeof (Header);
			}
			if (className.Equals("Question")){
				return typeof (Question);
			}
			else{
				return Type.GetType(string.Format( "{0}, {1}", typeName, assemblyName));
			}
		}	
	}
	
}

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