title: Read Multiple XML Elements And Write To SQL In C#
body: <p>Below I have my XML that I am receiving from a web-service.  I am really new with working with XML in C# but I am trying to read the <code>&lt;data_text&gt;</code> elements only when they are from a certain <code>&lt;form_id&gt;</code>.  I want to iterate through all of the XML to write to SQL what was sent.  Is there an easy way to set this up so I can find the <code>&lt;data_text&gt;</code>  say from a specific <code>&lt;field_number&gt;</code> and assign it to a String, then use that String to write to SQL?  Once I can get everything into a String I can easily write to SQL but I cannot get the XML into Strings.  I am open to other options as well.  At the bottom I have the current C# of where I am so far with this.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;!DOCTYPE pnet_message_history_packet_response PUBLIC&gt;
  &lt;pnet_message_history_packet_response&gt;
      &lt;packet_id&gt;2&lt;/packet_id&gt;
      &lt;imessage&gt;
          &lt;vehicle_number&gt;Test1&lt;/vehicle_number&gt;
          &lt;created_datetime&gt;02/20/2017 19:33:28&lt;/created_datetime&gt;
          &lt;received_datetime&gt;02/20/2017 19:33:53&lt;/received_datetime&gt;
          &lt;recipient&gt;
              &lt;recip_uid&gt;1234&lt;/recip_uid&gt;
              &lt;recip_name&gt;TestRecip&lt;/recip_name&gt;
          &lt;/recipient&gt;
          &lt;msn&gt;1233&lt;/msn&gt;
          &lt;base_msn&gt;1234&lt;/base_msn&gt;
          &lt;message_type&gt;form&lt;/message_type&gt;
          &lt;formdata&gt;
              &lt;form_id&gt;55555&lt;/form_id&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;5&lt;/field_number&gt;
                  &lt;empty_at_start&gt;no&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;no&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test5&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;6&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test6&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;7&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test7&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;8&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_date-time&gt;09/09/09 09:09:00&lt;/data_date-time&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;9&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_date-time&gt;09/09/09 09:09:00&lt;/data_date-time&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;10&lt;/field_number&gt;
                  &lt;empty_at_start&gt;no&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;no&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test10&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
          &lt;/formdata&gt;
      &lt;/imessage&gt;
      &lt;imessage&gt;
          &lt;vehicle_number&gt;Test1&lt;/vehicle_number&gt;
          &lt;created_datetime&gt;02/20/2017 19:34:04&lt;/created_datetime&gt;
          &lt;received_datetime&gt;02/20/2017 19:34:19&lt;/received_datetime&gt;
          &lt;recipient&gt;
              &lt;recip_uid&gt;1234&lt;/recip_uid&gt;
              &lt;recip_name&gt;TestRecip&lt;/recip_name&gt;
          &lt;/recipient&gt;
          &lt;msn&gt;1235&lt;/msn&gt;
          &lt;base_msn&gt;1236&lt;/base_msn&gt;
          &lt;message_type&gt;form&lt;/message_type&gt;
          &lt;formdata&gt;
              &lt;form_id&gt;55555&lt;/form_id&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;5&lt;/field_number&gt;
                  &lt;empty_at_start&gt;no&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;no&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test52&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;6&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test62&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;7&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test72&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;8&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_date-time&gt;09/08/09 09:08:00&lt;/data_date-time&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;9&lt;/field_number&gt;
                  &lt;empty_at_start&gt;yes&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;yes&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_date-time&gt;09/08/09 08:09:00&lt;/data_date-time&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
              &lt;im_field&gt;
                  &lt;field_number&gt;10&lt;/field_number&gt;
                  &lt;empty_at_start&gt;no&lt;/empty_at_start&gt;
                  &lt;driver_modified&gt;no&lt;/driver_modified&gt;
                  &lt;data&gt;
                      &lt;data_text&gt;Test102&lt;/data_text&gt;
                  &lt;/data&gt;
              &lt;/im_field&gt;
          &lt;/formdata&gt;
      &lt;/imessage&gt;
  &lt;/pnet_message_history_packet_response&gt;
  </code></pre>
  
  <p>Some C#-code</p>
  
  <pre><code>protected void GetMessages()
          {
              XmlDocument xmldoc = new XmlDocument();
              xmldoc.PreserveWhitespace = false;
  
              Regex regex = new Regex(@"^\s+$[\r\n]*");
              String cleanedXml = regex.Replace(postXMLData(URL, prefix, "POST"), "&gt;&lt;").TrimStart();
              xmldoc.LoadXml(cleanedXml);
  
              XmlNodeList messageList = xmldoc.GetElementsByTagName("imessage");
  
              foreach (XmlNode node in messageList)
              {
                  XmlElement messageElement = (XmlElement)node;
                  String Arrival;
  
                  Arrival = messageElement.GetElementsByTagName("data_text")[0].InnerText;
  
                  testTxtBx.Text += Arrival; //I am just trying to write to a Textbox now to see the results.
              }
          }
  </code></pre>