title: Serializing a c# object in yaml
body: <p>I'm trying to serialize a text who looks like that :</p>
  <pre><code>m_list 2 1035 6 16 204 206 501
  pdtse 1 6
  m_list 3 1 1027 10 1035 1 2038 8 -1
  pdtse 1 16
  m_list 3 1 1027 8 1035 1 2042 6 -1
  pdtse 1 204
  m_list 3 1 1027 15 1035 1 2042 10 -1
  pdtse 1 206
  m_list 3 1 1027 8 1035 1 2046 7 -1
  pdtse 1 501
  m_list 3 1 500 1 1027 14 1035 1 -1
  pdtclose
  </code></pre>
  <p>however, after few research &amp; debug I'm still getting the same results and can't figure out from where does the error come from.</p>
  <p>here is the result I got :</p>
  <p><a href="https://i.stack.imgur.com/wBXa9.png" rel="nofollow noreferrer">result</a></p>
  <p>but there is missing items &amp; random things such as *o0 &amp; &amp;o0 and I'm unable to figure out why</p>
  <p>here is my code :</p>
  <pre><code>        string[] lines = File.ReadAllLines(Path.Combine(_configuration.BasePath, _configuration.RecipeFile));
          List&lt;Recipe&gt; recipes = new();
          Recipe tempRecipe = new();
          long vnumToInsert = 0;
  
          foreach (string line in lines)
          {
              string[] parts = line.Split(' ');
  
              if (line.StartsWith(&quot;m_list 2&quot;))
              {
                  foreach (string part in parts.Skip(3))
                  {
                      if (tempRecipe.Recipes == null)
                      {
                          tempRecipe.Recipes = new List&lt;RecipeAttributes&gt;()
                          {
                              new RecipeAttributes
                              {
                                  ItemVnum = Convert.ToInt64(part),
                                  ProducerItemVnum = Convert.ToInt64(parts[2]),
                                  Amount = 1
                              }
                          };
                      }
                      else
                      {
                          tempRecipe.Recipes.Add(new RecipeAttributes
                          {
                              ItemVnum = Convert.ToInt64(part),
                              ProducerItemVnum = Convert.ToInt64(parts[2]),
                              Amount = 1
                          });
                      }
                  }
              }
              
              if (line.StartsWith(&quot;pdtse&quot;))
              {
                  vnumToInsert = Convert.ToInt64(parts[2]);
              }
  
              if (line.StartsWith(&quot;m_list 3&quot;))
              {
                  for (int i = 3; i &lt; parts.Length; i += 2)
                  {
                      if (Convert.ToInt64(parts[i]) &lt; 0) continue;
  
                      if (tempRecipe.Recipes == null) continue;
  
                      var findRecipeRelatedToVnum = tempRecipe.Recipes.FirstOrDefault(s =&gt; s.ItemVnum == vnumToInsert);
  
                      if (findRecipeRelatedToVnum == null) break;
  
                      if (findRecipeRelatedToVnum.Items == null)
                      {
                          findRecipeRelatedToVnum.Items = new List&lt;ItemAttributes&gt;()
                          {
                              new ItemAttributes()
                              {
                                  ItemVNum = Convert.ToInt64(parts[i]),
                                  Amount = Convert.ToInt64(parts[i + 1])
                              }
                          };
                      }
                      else
                      {
                          if (findRecipeRelatedToVnum.Items.FirstOrDefault(s =&gt; s.ItemVNum == Convert.ToInt64(parts[i])) != null) continue;
  
                          findRecipeRelatedToVnum.Items.Add(new ItemAttributes()
                          {
                              ItemVNum = Convert.ToInt64(parts[i]),
                              Amount = Convert.ToInt64(parts[i + 1])
                          });
                      }
                  }
              }
  
              if (line.StartsWith(&quot;pdtclose&quot;))
              {
                  recipes.Add(tempRecipe);
                  tempRecipe = new();
              }
          }
  
  
          Recipe toSerialize = new();
          List&lt;long&gt; alreadyDone = new();
          foreach (Recipe recipe in recipes)
          {
              toSerialize = new();
  
              if (recipe.Recipes == null) continue;
  
              if (alreadyDone.Contains(recipe.Recipes[0].ProducerItemVnum)) continue;
  
              foreach (Recipe r in recipes.Where(s =&gt; s.Recipes?.First().ProducerItemVnum == recipe.Recipes?.First().ProducerItemVnum))
              {
                  toSerialize = r;
              }
  
              alreadyDone.Add(recipe.Recipes[0].ProducerItemVnum);
              var serializer = new SerializerBuilder().Build();
              var yaml = serializer.Serialize(toSerialize);
              Console.WriteLine(yaml);
          }
  </code></pre>
  <p>and there is my yaml class :</p>
  <pre><code>using YamlDotNet.Serialization;
  
  namespace YamlParser.Entities
  {
      public record Recipe
      {
          [YamlMember(Alias = &quot;recipes&quot;)]
          public List&lt;RecipeAttributes&gt; Recipes { get; set; }
      }
  
      public record RecipeAttributes
      {
          [YamlMember(Alias = &quot;item_vnum&quot;)]
          public long ItemVnum { get; set; }
  
          [YamlMember(Alias = &quot;quantity&quot;)]
          public long Amount { get; set; }
  
          [YamlMember(Alias = &quot;producer_item_vnum&quot;)]
          public long ProducerItemVnum { get; set; }
  
          [YamlMember(Alias = &quot;items&quot;)]
          public List&lt;ItemAttributes&gt; Items { get; set; }
      }
  
      public record ItemAttributes
      {
          [YamlMember(Alias = &quot;item_vnum&quot;)]
          public long ItemVNum { get; set; }
  
          [YamlMember(Alias = &quot;quantity&quot;)]
          public long Amount { get; set; }
      }
  }
  </code></pre>
  <p>Thanks in advance for your help &amp; have a good day !</p>