title: Parse an XML as fast as possible
body: <p>I have a VB.Net app that reads a zip file that has an XML file in it.  I need to parse the XML file into row segments, pull one node value out as the application id and send it on it's way to a MS SQL database.  The XML file looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;PROJECTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;row&gt;
  &lt;APPLICATION_ID&gt;9243987&lt;/APPLICATION_ID&gt;
  &lt;ACTIVITY&gt;P30&lt;/ACTIVITY&gt;
  &lt;ADMINISTERING_IC&gt;AR&lt;/ADMINISTERING_IC&gt;
  &lt;APPLICATION_TYPE&gt;5&lt;/APPLICATION_TYPE&gt;
  &lt;ARRA_FUNDED&gt;N&lt;/ARRA_FUNDED&gt;
  &lt;AWARD_NOTICE_DATE&gt;05/22/2017&lt;/AWARD_NOTICE_DATE&gt;
  &lt;BUDGET_START&gt;04/01/2017&lt;/BUDGET_START&gt;
  &lt;/row&gt;
  &lt;row&gt;
  &lt;APPLICATION_ID&gt;9243988&lt;/APPLICATION_ID&gt;
  &lt;ACTIVITY&gt;P30&lt;/ACTIVITY&gt;
  &lt;ADMINISTERING_IC&gt;AR&lt;/ADMINISTERING_IC&gt;
  &lt;APPLICATION_TYPE&gt;5&lt;/APPLICATION_TYPE&gt;
  &lt;ARRA_FUNDED&gt;N&lt;/ARRA_FUNDED&gt;
  &lt;AWARD_NOTICE_DATE&gt;05/22/2017&lt;/AWARD_NOTICE_DATE&gt;
  &lt;BUDGET_START&gt;04/01/2017&lt;/BUDGET_START&gt;
  &lt;/row&gt;
  &lt;row&gt;
  &lt;APPLICATION_ID&gt;9243989&lt;/APPLICATION_ID&gt;
  &lt;ACTIVITY&gt;P30&lt;/ACTIVITY&gt;
  &lt;ADMINISTERING_IC&gt;AR&lt;/ADMINISTERING_IC&gt;
  &lt;APPLICATION_TYPE&gt;5&lt;/APPLICATION_TYPE&gt;
  &lt;ARRA_FUNDED&gt;N&lt;/ARRA_FUNDED&gt;
  &lt;AWARD_NOTICE_DATE&gt;05/22/2017&lt;/AWARD_NOTICE_DATE&gt;
  &lt;BUDGET_START&gt;04/01/2017&lt;/BUDGET_START&gt;
  &lt;/row&gt;
  &lt;/PROJECTS&gt;
  </code></pre>
  
  <p>The file could contain a million records and be close to 100mg in size.  My current code isbelow and could take as long as 8 hours to run for a million records.</p>
  
  <p>My VB code that parses the file is:</p>
  
  <pre><code>            If ofdXML.ShowDialog &lt;&gt; Windows.Forms.DialogResult.Cancel Then
              stopWatch.Start()
              Dim result As String
              Dim fName As String = ofdXML.FileName
              If fName.EndsWith("zip") Then
                  Dim ePath As String = "E:\Downloads\WEEKLY"
                  fileName = ExtractArchive(fName, ePath)
                  fName = Path.Combine(ePath, fileName)
              End If
  
              result = Path.GetFileNameWithoutExtension(fName)
              Dim rdr As New StreamReader(fName)
              While (rdr.Peek &gt;= 0)
                  varLine = rdr.ReadLine
                  sTag = varLine.Contains("&lt;row&gt;")
                  eTag = varLine.Contains("&lt;/row&gt;")
                  If sTag And eTag Then
                      appLine = varLine
                      If appLine.Contains("&lt;row&gt;&lt;APPLICATION_ID&gt;") Then
                          appID = appLine.Substring(Len("&lt;row&gt;&lt;APPLICATION_ID&gt;"), appLine.IndexOf("/APPLICATION_ID") - Len("&lt;row&gt;&lt;APPLICATION_ID&gt;") - 1)
                      End If
                  ElseIf sTag Then
                      v1 = True
                      appLine = varLine
                      If appLine.Contains("&lt;row&gt;&lt;APPLICATION_ID&gt;") Then
                          appID = appLine.Substring(Len("&lt;row&gt;&lt;APPLICATION_ID&gt;"), appLine.IndexOf("/APPLICATION_ID") - Len("&lt;row&gt;&lt;APPLICATION_ID&gt;") - 1)
                      End If
                  ElseIf eTag Then
                      appLine = appLine &amp; varLine
                      v1 = False
                  ElseIf v1 Then
                      appLine = appLine &amp; varLine
                      If appLine.Contains("&lt;APPLICATION_ID&gt;") Then
                          Dim xi As Integer = appLine.IndexOf("_ID&gt;") + 4
                          appID = appLine.Substring(xi, appLine.IndexOf("/APPLICATION_ID") - (xi + 1))
                      End If
                  End If
  
  
                  If Trim(Len(varLine)) &gt; 0 And appLine.Contains("&lt;row&gt;") And appLine.Contains("&lt;/row") And Not varLine.Contains("&lt;/PROJECTS&gt;") Then
                      TextBox2.Text = i.ToString
                      TextBox3.Text = appID
                      sb.Append(appID + ",")
                      Application.DoEvents()
                      i += 1
                      ADMIN_Save_To_Database(appLine, appID, result)
                  End If
              End While
  
          End If
  </code></pre>
  
  <p>Any help is greatly appreciated.</p>