title: Iterating through for loop but getting only one result
body: <p>I am trying to iterate every element under <strong>PcsInItem</strong> and get <strong>Price</strong> for each and insert them into SQL table. Below is my XML structure.</p>
  
  <pre><code>        &lt;Message&gt;
              &lt;Body&gt;
                  &lt;Item Id="8683297523"&gt;
                      &lt;PcsInItem&gt;
                          &lt;Pcs Price="20" PcsId="12829"&gt;&lt;/Pcs&gt;
                          &lt;Pcs Price="50" PcsId="64838"&gt;&lt;/Pcs&gt;
                          &lt;Pcs Price="80" PcsId="43829"&gt;&lt;/Pcs&gt;
                      &lt;/PcsInItem&gt;
                  &lt;/Item&gt;
              &lt;/Body&gt;
          &lt;/Message&gt;
  </code></pre>
  
  <p>When i run the code, it only inserts first item into SQL. I couldn't figure out why it does not iterate other items as well and only getting the first one.</p>
  
  <p>This is my code:</p>
  
  <pre><code>var xdoc = XDocument.Parse(soapResult);
  var y = xdoc.Descendants("Body").Descendants("PcsInItem");
  
  var a = xdoc.Descendants("Body");
  XElement b = a.Elements("Item").Last();
  var ItemId = b.Attributes("Id").FirstOrDefault().Value;
  
  SqlConnection conn;
  string constring = @"...";
  string query = "INSERT INTO..";
  
  using (conn = new SqlConnection(constring))
  {
      foreach (XElement x in y)
      {
          var Price = x.Element("Pcs").Attribute("Price")?.Value;
          var PcsId = x.Element("Pcs").Attribute("PcsId")?.Value;
          using (var cmd = new SqlCommand(query, conn))
          {
              try
              {
                  conn.Open();
                  cmd.Parameters.AddWithValue("@Price", Price);
                  cmd.Parameters.AddWithValue("@PcsId", PcsId);
                  cmd.Parameters.AddWithValue("@ItemId", ItemId);
                  komut2.ExecuteNonQuery();
              }
              catch (Exception e)
              {
                  Console.WriteLine(e);
              }
          }
      }
  }
  </code></pre>
  
  <p>Any idea why i am only getting first item in my SQL table? There should be 3 different table record and all these three record have same ItemId bu different Price and PcsId.</p>