Click here to Skip to main content
15,894,896 members
Articles / Programming Languages / XML

A Custom .NET XML Serialization Library

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
25 Jan 200611 min read 42.3K   432   21  
Describes a custom XML serialization library, with functionality to compare for, and to combine differences
using System;
using System.Collections;
using System.IO;

using Wxv.Wml;
using Wxv.Wml.IO;
using Wxv.Wml.Serialization;

namespace Wxv.WmlDemo
{
	public class DataObject : IWmlSerializable, IWmlSerializableCollection
	{
		public enum ExampleEnumaration { EnumValue0 = 0, EnumValue1 = 1, EnumValue2 = 2 }

		private const int MaxDepth = 3;
		private const int MaxCount = 3;

		private static int MaxId = 0;
		private static System.Random Random = new System.Random(0);

		private int depth = 0;
		public int Depth 
		{
			get { return depth; }
		}

		private int id = -1;
		[WmlAttribute()]
		public int Id
		{
			get { if (id == -1) id = MaxId++; return id; }
			set { id = value; }
		}

		[WmlAttribute()]
		public DateTime ExampleDateField = DateTime.Now;

		[WmlAttribute()]
		public ExampleEnumaration ExampleEnumarationField = ExampleEnumaration.EnumValue0;
		
		private int exampleIntProperty = Random.Next (100);
		[WmlAttribute()]
		public int ExampleIntProperty
		{
			get { return exampleIntProperty; }
			set { exampleIntProperty = value; }
		}

		[WmlAttribute()]
		public string ExampleStringField = Random.Next (2) == 0 ? Random.Next (100).ToString() : null;

		public override int GetHashCode()
		{
			return Id;
		}

		private WmlDictionary items = new WmlDictionary();

		public DataObject Get(int id)
		{
			return (DataObject) items.Get ("item", id);
		}

		IWmlSerializable IWmlSerializableCollection.Get(int id)
		{
			return Get (id);
		}

		public void Remove(int id)
		{
			items.Remove ("item", id);
		}

		public void Add (DataObject item)
		{
			items.Add ("item", item.Id, item);
			item.depth = depth + 1;
		}

		void IWmlSerializableCollection.Add (IWmlSerializable item)
		{
			Add ((DataObject) item);
		}

		public IEnumerator GetEnumerator()
		{
			return items.GetEnumerator();
		}

		public override string ToString()
		{
			return WmlSerializer.ToString (this);
		}

		public void Randomize()
		{
			ExampleDateField = DateTime.Now;

			switch (Random.Next (3))
			{
				case 0 : exampleIntProperty = Random.Next (100); break;
				case 1 : ExampleStringField = Random.Next (3) == 0 ? null : Random.Next (100).ToString(); break;
				case 2 : ExampleEnumarationField =  (ExampleEnumaration) Random.Next (3); break;
				default : break;
			}

			if ((Depth < MaxDepth) && (items.Count < MaxCount) && (Random.Next (2 + Depth) == 0))
			{
				DataObject t = new DataObject ();
				Add (t);
			}
			
			if ((Depth < MaxDepth) && (items.Count > 0) && (Random.Next (4 + Depth) == 0))
				items.RemoveAt (Random.Next (items.Count));

			foreach (DataObject dataObject in items)
				dataObject.Randomize();
		}
	}

}

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
New Zealand New Zealand
Im a Software Developer working in Auckland, New Zealand. When i was a lot shorter, i started programming in Atari Basic, though these days its mostly C#, and a bit of java (mostly the caffinated kind).

Comments and Discussions