Serializing a stack to XML in C#

Serializing a class to XML in C# is straightforward; most collections can be easily serialized also.

Something like the following will work in most scenarios -

 1public string GetSerializedString<T>(T objectToSerialize)
 3    var serializer = new XmlSerializer(typeof(T));
 4    TextWriter textWriter = new StringWriter();
 6    var xmlWriter = XmlWriter.Create(textWriter);
 7    serializer.Serialize(xmlWriter, objectToSerialize);
 9    string result = textWriter.ToString();
10    return result;

This also works nicely with lists, arrays and the like. But if you want to serialize a stack to XML you get an InvalidOperationException You must implement a default accessor on System.Collections.Generic.Stack`1[[System.String, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]] because it inherits from ICollection.

There might be some third party libraries out there that will work but I’m going to stick with Microsoft technologies.

So I need to add an accessor to a stack, but a quick look at the System.Collections.Generic.Stack class using dotPeek shows that the underlying collection is a private array.

1namespace System.Collections.Generic
3    public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
4    {
5        private T\[\] _array;
6    ...snip...

I could inherit from the Stack class, access the private array through reflection and add the required interface and methods to my class, or I could write my own stack class, but I don’t like these approaches. An alternative is to start with a collection that can be serialized to XML successfully and add the required stack functionality, peek, pop and push, to it. This a relatively straightforward approach, is easy to understand and provides a familiar interface. I chose to use the List as my base class.

 1using System.Collections.Generic;
 2using System.Linq;
 4namespace SerializingStack
 6    public class StackList<T> : List<T>
 7    {
 8        #region Public Methods
10        public void Push(T historyItem)
11        {
12            Add(historyItem);
13        }
15        public T Pop()
16        {
17            if (this.Any())
18            {
19                T historyItem = base[Count - 1];
20                Remove(historyItem);
21                return historyItem;
22            }
24            return default(T);
25        }
27        public T Peek()
28        {
29            if (this.Any())
30            {
31                return base[Count - 1];
32            }
33            return default(T);
34        }
36        #endregion
37    }

A problem with this approach is that order that items come out of the enumerator is the opposite from a traditional stack, but as long as the stack is accessed using peek, pop and push it will work the same. Compromises have to be accepted with mixing the features of two different types.

After deserialization

If you have never deserialized a stack then you can just use the StackList with no problems.

If you are used to working with stack, you’ll know that a stack create from another Stack will have its contents reversed. To overcome this you can call the Reverse method on the new stack. This is due to the way the stack constructor is written.

 1public Stack(IEnumerable<T> collection)
 3    ...snip...
 4    ICollection<T> collection1 = collection as ICollection<T>;
 5    if (collection1 != null)
 6    {
 7        ...snip...
 8        collection1.CopyTo(this._array, 0);
 9        ...snip...
10    }

If you want to overcome this issue you will need to implement your own enumerator functionality, this is not very difficult, but I didn’t need to do it for my purposes so I’m not including it here.


Be careful as you create your own types that inherit from Microsoft types, especially when you alter how parts of them work! Every step you take away from the standard approach you are getting in to trouble, it might not be apparent immediately, but it will catch up with you. This StackList is a cross between and a stack and list that behaves in some particular/peculiar ways that suit me for a for a limited purpose, I don’t plan to use this code widely.

comments powered by Disqus