Click here to Skip to main content
15,897,291 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.Xml;
using System.IO;
using System.Text;

namespace Wxv.Wml.IO
{
	/// <summary>
	/// A WmlReader class which uses an XmlReader reader to read Wml from
	/// an textual Xml source (e.g. a file, stream, XmlDocument or string)
	/// </summary>
	public class WmlTextReader : WmlReader
	{
		private string name = Wml.NameDefault;
		public override string Name { get { return name; } }

		private int id = Wml.IdDefault;
		public override int Id { get { return id; } }

		private string val = Wml.ValueDefault;
		public override string Value { get { return val; } }

		private string deletedValue = Wml.DeletedValueDefault;
		public override string DeletedValue { get { return deletedValue; } }

		private WmlNodeState state = Wml.StateDefault;
		public override WmlNodeState State { get { return state; } }

		private WmlReadState readState = WmlReadState.BOF;
		public override WmlReadState ReadState { get { return readState; } }

		private XmlReader reader;
		private int count = 0;
		private bool emptyElement = false;

		public WmlTextReader (XmlReader reader)
		{
			this.reader = reader;
		}

		public WmlTextReader (TextReader textReader) : this (new XmlTextReader (textReader))
		{
		}

		public WmlTextReader (string s) : this (new StringReader (s))
		{
		}

		public WmlTextReader (XmlDocument doc) : this (new XmlNodeReader (doc))
		{
		}

		public override bool Read()
		{
			name = Wml.NameDefault;
			id = Wml.IdDefault;
			val = Wml.ValueDefault;
			deletedValue = Wml.DeletedValueDefault;
			state = Wml.StateDefault;

			if (emptyElement)
			{
				emptyElement = false;
				readState = WmlReadState.EndNode;
				return true;
			}

			while (reader.Read()) 
			{
				switch (reader.NodeType)
				{
					case XmlNodeType.Element: 
					{
						emptyElement = reader.IsEmptyElement;
						name = reader.Name;
						if (reader.HasAttributes)
							while (reader.MoveToNextAttribute())
							{
								if (reader.Name == Wml.IdAttributeName)
									id = int.Parse (reader.Value);
								else if (reader.Name == Wml.ValueAttributeName)
									val = reader.Value;
								else if (reader.Name == Wml.DeletedValueAttributeName)
									deletedValue = reader.Value;
								else if (reader.Name == Wml.StateAttributeName)
									state = Wml.ParseState (reader.Value);
							}
						readState = WmlReadState.BeginNode;

						if ((count == 0) && (name != Wml.DocumentName))
							throw new Exception ("Not a Wml Document");

						count++;
						return true;
					}
					case XmlNodeType.EndElement:
					{
						readState = WmlReadState.EndNode;
						return true;
					}
					default :
					{
						continue;
					}
				} 
			}

			if (count == 0)
				throw new Exception ("Not a Wml Document");

			readState = WmlReadState.EOF;
			return false;
		}
	}
}

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