Return to Snippet

Revision: 46916
at May 27, 2011 12:13 by aelesbao


Initial Code
/// <summary>
/// <see cref="ICollection{T}"/> implementation which also provides xml serialization
/// </summary>
/// <typeparam name="T">The type of the contained elements</typeparam>
public abstract class XmlSerializableCollection<T> : ICollection<T>, IXmlSerializable
{
	#region Fields

	private readonly IList<T> items = new List<T>();

	#endregion

	#region Properties

	/// <summary>
	/// Obtains / Sets the element in a given ordinal position
	/// </summary>
	/// <param name="index">The ordinal position</param>
	public T this[int index]
	{
		get { return items[index]; }
		set { items[index] = value; }
	}

	#endregion

	#region ICollection<T> Members

	public void Add( T item )
	{
		items.Add( item );
	}

	public void Clear()
	{
		items.Clear();
	}

	public bool Contains( T item )
	{
		return items.Contains( item );
	}

	public void CopyTo( T[] array, int arrayIndex )
	{
		items.CopyTo( array, arrayIndex );
	}

	public int Count
	{
		get { return items.Count; }
	}

	public bool IsReadOnly
	{
		get { return items.IsReadOnly; }
	}

	public bool Remove( T item )
	{
		return items.Remove( item );
	}

	#endregion

	#region IEnumerable<T> Members

	public IEnumerator<T> GetEnumerator()
	{
		return items.GetEnumerator();
	}

	#endregion

	#region IEnumerable Members

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

	#endregion

	#region IXmlSerializable Members

	public virtual XmlSchema GetSchema()
	{
		return null;
	}

	public virtual void ReadXml( XmlReader reader )
	{
		XmlSerializerHelper.DeserializeCollection( reader, items );
	}

	public virtual void WriteXml( XmlWriter writer )
	{
		XmlSerializerHelper.Serialize( writer, this );
	}

	#endregion
}

Initial URL

                                

Initial Description
[Serializable]
[XmlRoot("items")]
public class ItemCollection : XmlSerializableCollection<Item>
{
}

Initial Title
XML Serializable Collection

Initial Tags
xml

Initial Language
C#