Serialization to and from XML document text using XmlSerializer object

Description and code samples
Table of Contents
Operations
Serialize/De-serialize to/from XML string/object using XmlWriter/XmlTextWriter and MemoryStream
How to do it
// BUILD COLLECTION 

List<Book> lvLibrary = new List<Book>();

Book lvBook = new Book();
lvBook.Author = "João Seixas";
lvBook.Title = "Training serialization book";
lvLibrary.Add(lvBook);

lvBook = new Book();
lvBook.Author = "João Seixas";
lvBook.Title = "Training serialization book (Second edition)";
lvLibrary.Add(lvBook);

// SERIALIZE OBJECT (encoding UTF-8)

XmlSerializer serializer = new XmlSerializer(typeof(List<Book>), new XmlRootAttribute("Library"));

XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Indent = true;
wSettings.Encoding = Encoding.UTF8;
wSettings.OmitXmlDeclaration = false;

using (MemoryStream ms = new MemoryStream())
{
	using (XmlWriter xWriter = XmlWriter.Create(ms, wSettings))
	{
		serializer.Serialize(xWriter, lvLibrary);
	}

    Byte[] buffer = new Byte[ms.Length];
    buffer = ms.ToArray();
    xml.Text = System.Text.Encoding.UTF8.GetString(buffer);
}

// DE-SERIALIZING XML

UTF8Encoding encoding = new UTF8Encoding();
byte[] buffer2 = encoding.GetBytes(xml.Text);

XmlReaderSettings rSettings = new XmlReaderSettings();

using (MemoryStream ms = new MemoryStream(buffer2))
{
	using (XmlReader xReader = XmlReader.Create(ms, rSettings))
	{
    lvLibrary = (List<Book>)serializer.Deserialize(ms);
    }
}
Serialized object:

<?xml version="1.0" encoding="utf-8"?>
<Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Book>
    <Title>Training serialization book</Title>
    <Author>João Seixas</Author>
  </Book>
  <Book>
    <Title>Training serialization book (Second edition)</Title>
    <Author>João Seixas</Author>
  </Book>
</Library>
Serialize/De-serialize to/from XML string/object using XmlWriter/XmlTextWriter and StringWriter/StringReader
Remarks

Since strings are stored with encoding UTF-16 in NET, the encoding attribute in the saved document is UTF-16 regardless of the encoding used.

How to do it
// BUILD COLLECTION

List<Book> lvLibrary = new List<Book>();

Book lvBook = new Book();
lvBook.Author = "João Seixas";
lvBook.Title = "Training serialization book";
lvLibrary.Add(lvBook);

lvBook = new Book();
lvBook.Author = "João Seixas";
lvBook.Title = "Training serialization book (Second edition)";
lvLibrary.Add(lvBook);

// SERIALIZE OBJECT (try to force encoding UTF-8)

XmlSerializer serializer = new XmlSerializer(typeof(List<Book>),new XmlRootAttribute("Library"));

XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Encoding = new UTF8Encoding(false, false); // no BOM in a .NET string
wSettings.Indent = true;
wSettings.OmitXmlDeclaration = false;

using (StringWriter sw = new StringWriter())
{
	using (XmlWriter xmlWriter = XmlWriter.Create(sw, wSettings))
	{
		serializer.Serialize(xmlWriter, lvLibrary);
	}

	xml.Text = sw.ToString();
}

// DE-SERIALIZING XML

XmlReaderSettings rSettings = new XmlReaderSettings();

using (StringReader textReader = new StringReader(xml.Text))
{
	using (XmlReader xReader = XmlReader.Create(textReader, rSettings))
	{
		List<Book> lvLibrary2= (List<Book>) serializer.Deserialize(xReader);
	}
}
<?xml version="1.0" encoding="utf-16"?>
<Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Book>
    <Title>Training serialization book</Title>
    <Author>João Seixas</Author>
  </Book>
  <Book>
    <Title>Training serialization book (Second edition)</Title>
    <Author>João Seixas</Author>
  </Book>
</Library>
Serialize/De-serialize to/from XML string/object using XmlWriter/XmlTextWriter and custom encoded StringWriter/StringReader to force encoding
How to do it
public sealed class StringWriterWithEncoding : StringWriter
{
	private readonly Encoding encoding;

	public StringWriterWithEncoding(Encoding encoding)
	{
		this.encoding = encoding;
	}

	public override Encoding Encoding
	{
		get { return encoding; }
	}
}

// BUILD COLLECTION

List<Book> lvLibrary = new List<Book>();

Book lvBook = new Book();
lvBook.Author = "João Seixas";
lvBook.Title = "Training serialization book";
lvLibrary.Add(lvBook);

lvBook = new Book();
lvBook.Author = "João Seixas";
lvBook.Title = "Training serialization book (Second edition)";
lvLibrary.Add(lvBook);

// SERIALIZE OBJECT (force UTF-8)

XmlSerializer serializer = new XmlSerializer(typeof(List<Book>), new XmlRootAttribute("Library"));

XmlWriterSettings wSettings = new XmlWriterSettings();
wSettings.Encoding = new  UnicodeEncoding(false, false); // no BOM in a .NET string
wSettings.Indent = true;
wSettings.OmitXmlDeclaration = false;

using (StringWriterWithEncoding sw = new StringWriterWithEncoding(new UTF8Encoding()))
{
	using (XmlWriter xWriter = XmlWriter.Create(sw, wSettings))
	{
		serializer.Serialize(xWriter, lvLibrary);
	}

	xml.Text = sw.ToString();
}

// DE-SERIALIZING XML

XmlReaderSettings rSettings = new XmlReaderSettings();

using (StringReader sr = new StringReader(xml.Text))
{
	using (XmlReader xReader = XmlReader.Create(sr, rSettings))
    {
		List<Book> lvLibrary2 = (List<Book>)serializer.Deserialize(xReader);
	}
}
<?xml version="1.0" encoding="utf-8"?>
<Library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Book>
    <Title>Training serialization book</Title>
    <Author>João Seixas</Author>
  </Book>
  <Book>
    <Title>Training serialization book (Second edition)</Title>
    <Author>João Seixas</Author>
  </Book>
</Library>