title: MailAttachment Serialization and Deserialization not working
body: <p>I am trying to serialize a MailMessage object using implementation of IXmlSerializable interface. The serialized object is then stored in database (that is using SQL Server CE 3.5) using Image DataType.  Everything works fine on deserialization except the Attachment Collection. On deserilzing the Images are attached but do not show up correctly in the Email, Text files are empty.</p>
  <p>This is the code to deserialize (only the attachment list part)</p>
  <pre><code> // Attachments
              XmlNode attachmentsNode = GetConfigSection(xml, &quot;SerializableMailMessage/MailMessage/Attachments&quot;);
              if (attachmentsNode != null)
              {
                  foreach (XmlNode node in attachmentsNode.ChildNodes)
                  {
                      string contentTypeString = string.Empty;
                      if (node.Attributes[&quot;ContentType&quot;] != null)
                          contentTypeString = node.Attributes[&quot;ContentType&quot;].Value;
  
                      ContentType contentType = new ContentType(contentTypeString);
  
                      MemoryStream stream = new MemoryStream();
                      byte[] data = Encoding.UTF8.GetBytes(node.InnerText);
                      stream.Write(data, 0, data.Length);
  
                      Attachment attachment = new Attachment(stream, contentType);
                      this.Email.Attachments.Add(attachment);
                  }
              }
  
          private XmlNode GetConfigSection(XmlDocument xml, string nodePath)
          {
              return xml.SelectSingleNode(nodePath);
          }
  </code></pre>
  <p>and this is the code to serialize</p>
  <pre><code>// Attachments
                  if (this.AttachmentList!=null)
                  {
                      writer.WriteStartElement(&quot;Attachments&quot;);
  
                      foreach (Attachment attachment in this.AttachmentList)
                      {
                          writer.WriteStartElement(&quot;Attachment&quot;);
  
                          if (!string.IsNullOrEmpty(attachment.Name))
                              writer.WriteAttributeString(&quot;ContentType&quot;, attachment.ContentType.ToString());
  
                          using (BinaryReader reader = new BinaryReader(attachment.ContentStream))
                          {
                              byte[] data = reader.ReadBytes((int)attachment.ContentStream.Length);
  
                              writer.WriteBase64(data, 0, data.Length);
                          }
  
                          writer.WriteEndElement();
                      }
  
                      writer.WriteEndElement();
                  }
  </code></pre>
  <p>I got this code from the GOPI C# mail sending library on CodePlex
  <a href="http://gopi.codeplex.com/" rel="nofollow noreferrer">http://gopi.codeplex.com/</a></p>
  <p>Even in the issue tracker this is an issue. Kindly advise what may be going wrong.</p>
  <p><strong>EDIT 1</strong>: Sorry guys, I had posted my tryout code. The correct code is shown now.(in the serialize code at writer.WriteBase64(data, 0, data.Length);</p>