Generic methods for serializing and deserialzing xml files using streams using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace Common.Extensions
{
    /// &lt;summary&gt;
    /// Contains the logic for streaming extensions.
    /// &lt;/summary&gt;
    public static class StreamExtensions
    {
        /// &lt;summary&gt;
        /// Serialize an object.
        /// &lt;/summary&gt;
        /// &lt;typeparam name="T"&gt;The type of the object that gets serialized.&lt;/typeparam&gt;
        /// &lt;param name="stream"&gt;The stream to which the bytes will be written.&lt;/param&gt;
        /// &lt;param name="serializableObject"&gt;The object that gets serialized.&lt;/param&gt;
        public static void SerializeObject&lt;T&gt;(this Stream stream, T serializableObject) where T : IXmlSerializable
        {
            var writerSettings = new XmlWriterSettings();

            writerSettings.Indent = true;
            writerSettings.IndentChars = "    "; // 4 spaces
            writerSettings.Encoding = Encoding.UTF8;

            using (var xmlWriter = XmlWriter.Create(stream, writerSettings))
            {
                serializableObject.WriteXml(xmlWriter);
            }

            stream.Close();
        }

        /// &lt;summary&gt;
        /// Deserialize a stream and return the object.
        /// &lt;/summary&gt;
        /// &lt;typeparam name="T"&gt;The type of the object that returns from the deserialization.&lt;/typeparam&gt;
        /// &lt;param name="stream"&gt;The stream which contains the bytes to deserialize.&lt;/param&gt;
        /// &lt;returns&gt;The object recovered.&lt;/returns&gt;
        public static T DeserializeObject&lt;T&gt;(this Stream stream) where T : IXmlSerializable
        {
            using (var xmlTextReader = XmlReader.Create(stream))
            {
                var serializer = new XmlSerializer(typeof(T));
                T result = (T)serializer.Deserialize(xmlTextReader);
                stream.Close();
                return result;
            }
        }
    }
}


I have made a class which can serialize a generic object which implements the interface IXmlSerializable and can deserialize an xml file to that same object. The idea behind this is as follows: we are using a lot of xml files to (for example) write configurations to. Since the configuration classes are often different we needed a generic function to create and read the xml files.

The stream we use (atm) is a file stream since it is currently only used for reading and writing the xml files to and from disk. Later we might use other kind of streams as well.

I am wondering if what I have written is the correct way (obviously it works but can it be made better? )
writerSettings.IndentChars = "    "; // 4 spaces  



by using 

writerSettings.IndentChars = string.Empty.PadRight(4, ' ');  


you wouldn't need that noisy comment.  



One wouldn't expect that a passed in Stream get closed inside that method. The closing of a Stream should only belong to the creator of that stream.  



In extension methods you should always check the argument which is reffered by this if it is null.
This is because that method can also be called like so  

StreamExtensions.SerializeObject&lt;someType&gt;(null, someObject);  


By checking (stream == null) and throwing an ArgumentNullException no unnecessary objects like XmlWriterSettings will be created.  



Otherwise than the mentioned points your code looks clean and is easy to read.