Click here to Skip to main content
15,880,651 members
Articles / Programming Languages / XML

Towards a Declarative SAX Framework : Part 1 - A Simple SAX-to-C#-Mapping

Rate me:
Please Sign up or sign in to vote.
4.51/5 (15 votes)
20 May 200422 min read 62.7K   894   33  
This article demonstrates a simple but flexible way to read XML content without DOM
#region Copyright
/*********************************************************************************
 * Copyright (c) 2004 by Martin Friedrich										 *
 * Permission to freely distribute, modify and compile these sources and		 *
 * binaries as long as this copyright notice is included.						 *
 * Use of code on your risk!													 *
 * *******************************************************************************/
#endregion

using System;
using System.Text;
using System.IO;

namespace SaxParserSupport
{
	namespace Impl 
	{
		/// <summary>
		/// Implements <tt>XMLDocument</tt>. This class is abstract because it doesn't
		/// implement method <tt>SaxParserSupport.Impl.XMLElement.getElementClasses</tt>
		/// <seealso cref="XMLDocument"/>
		/// </summary>
		public abstract class XMLDocumentImpl : XMLElementImpl, XMLDocument
		{
			/// <summary>
			/// Constructs an instance of <tt>XMLDocumentImpl</tt>. The XML version is
			/// set to 1.0 and it's encoding is UTF-8 by default.
			/// </summary>
			protected XMLDocumentImpl()
			{
				_encoding = new UTF8Encoding().HeaderName;
				_version = "1.0";
			}

			/// <summary>
			/// Emits this <tt>XMLDocument</tt> instance to <tt>str</tt>
			/// </summary>
			/// <remarks>
			/// <list type="bullet"><item>As of now, only the XML declaration and the root element (and
			/// it's subselement, of course) are emitted.</item>
			/// <item>This method may throw various exceptions related to stream output</item>
			/// </list>
			/// <seealso cref="XMLDocument"/>
			/// </remarks>
			/// <param name="ostr">The output stream</param>
			public override void write( System.IO.StreamWriter ostr )
			{
				ostr.WriteLine( "<?xml version=\"{0}\" encoding=\"{1}\" ?>", _version, getEncoding() );
				base.write( ostr );
				ostr.Close();
			}

			/// <summary>
			/// Returns the name of the encoding to be used for output.
			/// </summary>
			/// <returns>The name of the encoding</returns>
			public string getEncoding()
			{
				return _encoding;
			}

			/// <summary>
			/// Sets the name of the encoding for output.
			/// </summary>
			/// <param name="en">The encoding's name as given by it's <tt>System.Text.Encoding.HeaderName</tt>
			/// property</param>
			public void setEncoding( string en )
			{
				_encoding = en;
			}

			/// <summary>
			/// Returns the XML version.
			/// </summary>
			/// <returns>The XML version in string form</returns>
			public string getVersion()
			{
				return _version;
			}

			/// <summary>
			/// Sets the XML version. The XML version should always be <tt>"1.0"</tt>.
			/// </summary>
			/// <param name="v">the XML version in string form</param>
			public void setVersion( string v )
			{
				_version = v;
			}

			/// <summary>
			/// ***PRIVATE***
			/// </summary>
			private string _encoding;

			/// <summary>
			/// ***PRIVATE***
			/// </summary>
			private string _version;
		}
	}
}

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany

Still lacking an university degree in computer science, I have 20 years of experience in software development and implementation. Having expert knowledge in object-oriented programming languages like C++, Java and C# on Windows, LINUX and UNIX platforms, I participated in multiple research projects at the University of Oldenburg. During these assignments, I was trusted with implementation of a graphical editor for specification languages like CSP or Z and a prototypical tool for workflow data distribution and analysis. I gained experiences in a widespread spectrum of CS and software development topics, ranging from compiler construction across data base programming to MDA. My research interests include questions of graphical user interface design and component-based systems.


I consider myself an old-school CS geek. While I admit that simple tasks do not pose much of a problem and can be done in quick and efficient manner, it's the challenging ones that appeal to me. If you are looking for a skillful employee - be it on a permanent, contract or freelance basis - and if you can live with a lacking university degree, why not contacting me?


Comments and Discussions