title: How to attach an Xml to .net Mail Attachment using MemoryStream?
body: <p>I have a cshtml view in mvc filled with xml such as :</p>
  
  <pre><code>@model myproject.net.Models.mymodel
  @{
      Layout = null;
      Response.ContentType = "application/vnd.ms-excel";
      Response.AddHeader("Content-Disposition", "attachment; " + "filename=" + 
          Model.myusername.ToString()  + "(" + 
          Model.mydate.Date.ToShortDateString() + ").xls");
  }
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"&gt;
      &lt;Styles&gt;
          &lt;Style ss:ID="s25"&gt;
              &lt;Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/&gt;
              &lt;Borders&gt;
                  &lt;Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1"/&gt;
              &lt;/Borders&gt;
              &lt;Interior ss:Color="#FFA500" ss:Pattern="Solid"/&gt;
          &lt;/Style&gt;
      &lt;/Styles&gt;
      &lt;ss:Worksheet ss:Name="Sheet1"&gt;
          &lt;ss:Table&gt;
          @if (Model.someNumerableContent.Count &gt; 0)
          {
              &lt;!-- MyContent --&gt;
              &lt;ss:Row&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;Content Header&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
              &lt;/ss:Row&gt;
              &lt;ss:Row&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;SubHeader 1&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;SubHeader 2&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;SubHeader 3&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;SubHeader 4&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;SubHeader 5&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
              &lt;/ss:Row&gt;
              foreach (var subContent in Model.someNumerableContent)
              {
              &lt;ss:Row&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;@subContent.mydate.Date.ToShortDateString()&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;@subContent.number&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;@subContent.name&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;@subContent.surname&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
                  &lt;ss:Cell&gt;&lt;ss:Data ss:Type="String"&gt;@subContent.issue&lt;/ss:Data&gt;&lt;/ss:Cell&gt;
              &lt;/ss:Row&gt;
              }
              &lt;ss:Row&gt; &lt;/ss:Row&gt;
          }
          &lt;/ss:Table&gt;
      &lt;/ss:Worksheet&gt;
  &lt;/ss:Workbook&gt;
  </code></pre>
  
  <p>and i wanna convert it to .xls file and attach to a mail without saving it anywhere.
  but couldnt figure out how can i achive this. I cant use office.interop for some restrictions i got in the server so its not an option to me. I only want to create an .xls file by Xml and send it using .net mail. so far i've done this:</p>
  
  <pre><code>// Memory stream for the xml file
  using (MemoryStream memoryStream = new MemoryStream())
  {
      //                                                 Can i use something like this?
      byte[] contentAsBytes = Encoding.Default.GetBytes( View("ExportToExcel").ToString() );
      memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
      // Set the position to the beginning of the stream.
      memoryStream.Seek(0, SeekOrigin.Begin);
      // Create attachment
      ContentType contentType = new ContentType();
      contentType.MediaType = MediaTypeNames.Text.Xml;
      contentType.Name = UserName + "(" + FileDate + ").xls";
      // Attach
      mail.Attachment = new Attachment(memoryStream, contentType);
  }
  </code></pre>
  
  <p>so how can i achive this?</p>