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

namespace Wxv.Wml.IO
{
	/// <summary>
	/// A WmlWriter class which uses an XmlWriter reader to write Wml to
	/// an Xml target (e.g. a file, stream, or string)
	/// </summary>
	public class WmlTextWriter : WmlWriter
	{
		private XmlWriter writer;

		private ArrayList elementCount = new ArrayList();

		public WmlTextWriter (XmlWriter writer) : base ()
		{
			this.writer = writer;
			this.elementCount.Add ((int) 1);
		}

		public WmlTextWriter (TextWriter textWriter) : this (new XmlTextWriter (textWriter))
		{
		}

		public override void WriteBeginNode (string name, int id, string val, string deletedValue, WmlNodeState state)
		{
			if (elementCount.Count == 0)
				throw new Exception ("Mismatched WriteBeginNode call in WmlTextWriter");

			if ((int) elementCount [elementCount.Count - 1] == 0)
				writer.WriteWhitespace("\r\n");
			if ((elementCount.Count - 1) > 0)
				writer.WriteWhitespace (new string (' ', (elementCount.Count - 1) * 2));

			elementCount.Add ((int) 0);

			writer.WriteStartElement (XmlConvert.EncodeName (name));
			if (id != Wml.IdDefault)
				writer.WriteAttributeString (Wml.IdAttributeName, id.ToString());
			if (val != Wml.ValueDefault)
				writer.WriteAttributeString (Wml.ValueAttributeName, val);
			if (deletedValue != Wml.DeletedValueDefault)
				writer.WriteAttributeString (Wml.DeletedValueAttributeName, deletedValue);
			if (state != Wml.StateDefault)
				writer.WriteAttributeString (Wml.StateAttributeName, Wml.ToString (state));
		}

		public override void WriteEndNode()
		{
			if (elementCount.Count == 0)
				throw new Exception ("Mismatched WriteEndNode call in WmlTextWriter");

			int count = (int) elementCount [elementCount.Count - 1];
			elementCount.RemoveAt (elementCount.Count - 1);
			if ((count > 0) && ((elementCount.Count - 1) > 0))
				writer.WriteWhitespace (new string (' ', (elementCount.Count - 1) * 2));
			
			if (elementCount.Count > 0)
				elementCount [elementCount.Count - 1] = (int) elementCount [elementCount.Count - 1] + 1;

			writer.WriteEndElement();
			writer.WriteWhitespace("\r\n");
		}

		public override void Dispose()
		{
			if (writer != null)
			{
				writer.Close();
				writer = null;
			}
		}
	}
}

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