title: write objects from array to text file c#
body: <p>This C# assignment is about creating groups for salesmen at diffrent levels depending how much they sold for, and then write this info to a textfile using a streamwriter.</p>
  
  <p>I have done all parts except the streamwriter part. What i do so far is to create the file, but there is no ouput.
  When i run this the content in the console is correct but the textfile becomes modified but blank. </p>
  
  <p>The streamwriter is in the bottom part. </p>
  
  <p>´</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text; 
  using System.Threading.Tasks;
  
  namespace Inlämningsuppgift2
  {
  
  // Emplooyeklassen where names, personal info, district och sold items sparas och hämtas
  
  class Emplooye
  {
  
      String namn;
      int personnummer;
      string distrikt;
      int såldaartiklar;
      int säljnivå;
  
  
      public Emplooye(string namn, int personnummer, string distrikt, int såldaartiklar)
      {
          this.namn = namn;
          this.personnummer = personnummer;
          this.distrikt = distrikt;
          this.såldaartiklar = såldaartiklar;
  
      }
  
      public string Name
      {
          get { return namn; }
          set { namn = value; }
      }
  
  
      public int PersonNummer
      {
          get { return personnummer; }
          set { personnummer = value; }
      }
  
  
      public string Distrikt
      {
          get { return distrikt; }
          set { distrikt = value; }
      }
  
  
      public int AmountSold
      {
          get { return såldaartiklar; }
          set { såldaartiklar = value; }
      }
  
      public int SäljNivå
      {
          get { return säljnivå; }
          set { säljnivå = value; }
      }
  }
   }
  
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Threading.Tasks;
   using Inlämningsuppgift2;
   using System.IO;
  
    namespace ConsoleApp3
  
  {
      class Säljlista
      {
          static void Main(string[] args)
          {
  
  
  
              List&lt;Emplooye&gt; ObjSeller = new List&lt;Emplooye&gt;();
              List&lt;Emplooye&gt; group1 = new List&lt;Emplooye&gt;();
              List&lt;Emplooye&gt; group2 = new List&lt;Emplooye&gt;();
              List&lt;Emplooye&gt; group3 = new List&lt;Emplooye&gt;();
  
              ObjSeller.Add(new Emplooye("Oscar Norberg", 961111, "Täby", 140));
              ObjSeller.Add(new Emplooye("jonas okembia", 970912, "Uppsala", 70));
              ObjSeller.Add(new Emplooye("Mille vega", 981212, "Danderyd", 40));
              ObjSeller.Add(new Emplooye("Isak friisjespesen", 991132, "Skåne", 80));
              ObjSeller.Add(new Emplooye("Maja olofsson", 974132, "Täby", 123));
              ObjSeller.Add(new Emplooye("Annmarie norberg", 944432, "Täby", 230));
              ObjSeller.Add(new Emplooye("Ulla persson", 9312332, "Uppland", 124));
              ObjSeller.Add(new Emplooye("Christer Nilsen", 9988332, "Vallentuna", 444));
  
              Console.WriteLine("Namn    Personnummer     Distrikt   Antal");
  
              //Here the employees gets sorted in groups depending how much they sold for.
  
  
              foreach (Emplooye e in ObjSeller)
              {
                  if (e.AmountSold &gt; 0 &amp;&amp; e.AmountSold &lt;= 90)
  
                  {
                      group1.Add(e);
  
                  }
  
                  else if (e.AmountSold &gt; 90 &amp;&amp; e.AmountSold &lt;= 200)
                  {
                      group2.Add(e);
                  }
                  else
                  {
                      group3.Add(e);
                  }
  
              }
  
  
              group1.Sort(delegate (Emplooye t1, Emplooye t2)
              { return (t1.AmountSold.CompareTo(t2.AmountSold)); }
              );
  
              for (int i = 0; i &lt; group1.Count; i++)
  
              {
  
                  string name = group1.ElementAt(i).Name;
                  int pnr = group1.ElementAt(i).PersonNummer;
                  String district = group1.ElementAt(i).Distrikt;
                  int amountsold = group1.ElementAt(i).AmountSold;
  
                  Console.WriteLine(name + ": " + pnr + " - " + district + " - " + amountsold);
  
              }
              Console.WriteLine("salesmen at level 1" + ": " + group1.Count);
              Console.WriteLine("");
  
  
              group2.Sort(delegate (Emplooye t1, Emplooye t2)
              { return (t1.AmountSold.CompareTo(t2.AmountSold)); }
             );
  
              for (int i = 0; i &lt; group2.Count; i++)
  
              {
  
                  string name = group2.ElementAt(i).Name;
                  int pnr = group2.ElementAt(i).PersonNummer;
                  String district = group2.ElementAt(i).Distrikt;
                  int amountsold = group2.ElementAt(i).AmountSold;
  
                  Console.WriteLine(name + ": " + pnr + " - " + district + " - " + amountsold);
  
              }
              Console.WriteLine("salesmen at level 2 2" + ": " + group2.Count);
              Console.WriteLine("");
  
  
              group3.Sort(delegate (Emplooye t1, Emplooye t2)
              { return (t1.AmountSold.CompareTo(t2.AmountSold)); }
         );
  
              for (int i = 0; i &lt; group3.Count; i++)
  
              {
  
                  string name = group3.ElementAt(i).Name;
                  int pnr = group3.ElementAt(i).PersonNummer;
                  String district = group3.ElementAt(i).Distrikt;
                  int amountsold = group3.ElementAt(i).AmountSold;
  
                  Console.WriteLine(name + ": " + pnr + " - " + district + " - " + amountsold);
  
              }
              Console.WriteLine("salesmen at level 3" + ": " + group3.Count);
              Console.WriteLine("");
  
  
          StreamWriter SW = new StreamWriter("important.txt");
          foreach(Emplooye i in group1)
          {
  
              SW.WriteLine(group1.ToString());
  
             foreach(Emplooye i in group2)
          {
  
              SW.WriteLine(group2.ToString());
          } 
              foreach(Emplooye i in group3)
          {
  
              SW.WriteLine(group3.ToString());
  
  
  
      }
  }
  
  
  
  
  
      }
  </code></pre>