title: Bulk uploading backlog of xml files to MVC Controller
body: <p>I have an MVC4 application that parses XML files specific to a particular data format, I have this working as expected when uploading the xml file through a file input control and it takes between 1-5 seconds to parse each file.</p>
  
  <p>If however I choose to upload a series of 50 files it seems to take 30-60 seconds per record. What would be the cause of this delay when bulk uploading the 50 files?</p>
  
  <p>I have a backlog of 2000 files to upload and obviously would like to automate this process somehow, any suggestions would be greatly appreciated</p>
  
  <pre><code>&lt;HttpPost()&gt; _
  &lt;ValidateAntiForgeryToken()&gt; _
  Function Create(ByVal file As HttpPostedFileBase) As ActionResult
      If ModelState.IsValid Then
  
          'Grab the uploaded file from the HttpRequest and save it to the MarketMessageFiles folder
          Dim xmlFile As HttpPostedFileBase
  
          Dim fileCount = Request.Files.Count
  
          For fileIteration = 0 To fileCount - 1
  
              xmlFile = Request.Files(fileIteration)
  
              Try
                  xmlFile.SaveAs(Server.MapPath("~/MarketMessageFiles/" + xmlFile.FileName))
              Catch ex As Exception
                  Return View()
              End Try
  
  
  
              'Declare Variables for XML Parsing
              'Dim xmlDoc As New XmlDocument()
              'Dim xmlNodeList As XmlNodeList
              Dim i As Integer
              Dim j As Integer
  
              'Declare Variables for Entity Creation Values
              Dim TxRefNbr = ""
              Dim RecipientID = ""
              Dim SenderID = ""
              Dim MarketTimestamp = ""
              Dim VersionNumber = ""
              Dim MessageTypeCode = ""
              Dim MPRN = ""
  
              'Declare Variable for the MarketMessage class
              Dim marketMessage = New MarketMessage
  
              'Create a FileStream from the file saved in the MarketMessagesFiles folder
              Dim fs As New FileStream(Server.MapPath("~/MarketMessageFiles/" + xmlFile.FileName), FileMode.Open, FileAccess.Read)
              xmlDoc.Load(fs)
              xmlNodeList = xmlDoc.GetElementsByTagName("ieXMLDocument")
  
              'Process the Header Message
              For i = 0 To xmlNodeList.Count - 1
  
                  For j = 0 To xmlNodeList(i).ChildNodes(0).Attributes.Count - 1
  
                      'Set the relevant variable according to the header attributes
                      Select Case xmlNodeList(i).ChildNodes(0).Attributes(j).Name
  
                          Case "TxRefNbr" : TxRefNbr = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                          Case "RecipientID" : RecipientID = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                          Case "SenderID" : SenderID = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                          Case "MarketTimestamp" : MarketTimestamp = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                          Case "VersionNumber" : VersionNumber = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                          Case "MessageTypeCode" : MessageTypeCode = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
                              'Case "MPRN" : MPRN = xmlNodeList(i).ChildNodes(0).Attributes(j).Value
  
                      End Select
  
                  Next
  
              Next
  
              'Set the MessageHeader attributes
              marketMessage.createdOn = Date.Today
              marketMessage.marketTimeStamp = DateTime.Parse(MarketTimestamp)
              marketMessage.recipient = RecipientID
              marketMessage.sender = SenderID
              'If (Not MPRN = "") Then
              '    marketMessage.mprn = CType(MPRN, Long)
              'End If
              marketMessage.messageType = MessageTypeCode
              marketMessage.txRefNbr = TxRefNbr
  
              'Add MarketMessage class to database context
              db.MarketMessages.Add(marketMessage)
              Try
                  db.SaveChanges()
              Catch ex As DbEntityValidationException
                  For Each validationErrors In ex.EntityValidationErrors
  
                      For Each validationError In validationErrors.ValidationErrors
                          Dim test = validationError.ErrorMessage
                      Next
                  Next
  
              End Try
  
  
  
              'Search the repository using the code and return the market message type
              Dim mmt As MarketMessageType = db.MarketMessageTypes.[Single](Function(_mmt) _mmt.Header = MessageTypeCode)
  
              'Check the MarketMessageTypeID and process according to the relevant message type
              Select Case mmt.MarketMessageTypeID
  
                  Case 1 : Process010(xmlNodeList)
                  Case 2 : Process011(xmlNodeList)
                  Case 3 : Process011A(xmlNodeList)
                  Case 4 : Process012(xmlNodeList)
                  Case 5 : Process012W(xmlNodeList)
                  Case 6 : Process013(xmlNodeList)
                  Case 7 : Process014R(xmlNodeList)
                  Case 8 : Process015(xmlNodeList)
                  Case 9 : Process016(xmlNodeList)
                  Case 10 : Process017(xmlNodeList)
                  Case 11 : Process021(xmlNodeList)
                  Case 12 : Process030(xmlNodeList)
                  Case 13 : Process101(xmlNodeList)
                  Case 14 : Process101P(xmlNodeList)
                  Case 15 : Process101R(xmlNodeList)
                  Case 16 : Process102(xmlDoc, marketMessage.MarketMessageID)
                  Case 17 : Process102P(xmlNodeList)
                  Case 18 : Process102R(xmlNodeList)
                  Case 19 : Process105(xmlNodeList)
                  Case 20 : Process105L(xmlNodeList)
                  Case 21 : Process106D(xmlNodeList)
                  Case 22 : Process106E(xmlNodeList)
                  Case 23 : Process110(xmlNodeList)
                  Case 24 : Process111(xmlNodeList)
                  Case 25 : Process111A(xmlNodeList)
                  Case 26 : Process111L(xmlNodeList)
                  Case 27 : Process111R(xmlNodeList)
                  Case 28 : Process112(xmlNodeList)
                  Case 29 : Process112R(xmlNodeList)
                  Case 30 : Process112W(xmlNodeList)
                  Case 31 : Process114(xmlNodeList)
                  Case 32 : Process115(xmlNodeList)
                  Case 33 : Process115R(xmlNodeList)
                  Case 34 : Process116(xmlNodeList)
                  Case 35 : Process116A(xmlNodeList)
                  Case 36 : Process116N(xmlNodeList)
                  Case 37 : Process116R(xmlNodeList)
                  Case 38 : Process117D(xmlNodeList)
                  Case 39 : Process117R(xmlNodeList)
                  Case 40 : Process122(xmlNodeList)
                  Case 41 : Process122R(xmlNodeList)
                  Case 42 : Process130D(xmlNodeList)
                  Case 43 : Process130R(xmlNodeList)
                  Case 44 : Process131(xmlNodeList)
                  Case 45 : Process137R(xmlNodeList)
                  Case 46 : Process140(xmlNodeList)
                  Case 47 : Process208(xmlNodeList)
                  Case 48 : Process210(xmlNodeList)
                  Case 49 : Process252(xmlNodeList)
                  Case 50 : Process260(xmlNodeList)
                  Case 51 : Process261(xmlNodeList)
                  Case 52 : Process300(xmlDoc, marketMessage.MarketMessageID)
                  Case 53 : Process300S(xmlDoc, marketMessage.MarketMessageID)
                  Case 54 : Process300W(xmlDoc, marketMessage.MarketMessageID)
                  Case 55 : Process301(xmlNodeList)
                  Case 56 : Process301N(xmlNodeList)
                  Case 57 : Process303R(xmlNodeList)
                  Case 58 : Process305(xmlDoc, marketMessage.MarketMessageID)
                  Case 59 : Process306(xmlDoc, marketMessage.MarketMessageID)
                  Case 60 : Process306W(xmlNodeList)
                  Case 61 : Process307(xmlNodeList)
                  Case 62 : Process307W(xmlNodeList)
                  Case 63 : Process308(xmlNodeList)
                  Case 64 : Process310(xmlDoc, marketMessage.MarketMessageID)
                  Case 65 : Process310W(xmlNodeList)
                  Case 66 : Process311(xmlNodeList)
                  Case 67 : Process320(xmlDoc, marketMessage.MarketMessageID)
                  Case 68 : Process320W(xmlDoc, marketMessage.MarketMessageID)
                  Case 69 : Process321(xmlNodeList)
                  Case 70 : Process330(xmlNodeList)
                  Case 71 : Process331(xmlNodeList)
                  Case 72 : Process332(xmlDoc, marketMessage.MarketMessageID)
                  Case 73 : Process332W(xmlNodeList)
                  Case 74 : Process341(xmlDoc, marketMessage.MarketMessageID)
                  Case 75 : Process342(xmlDoc, marketMessage.MarketMessageID)
                  Case 76 : Process352R(xmlNodeList)
                  Case 77 : Process507(xmlNodeList)
                  Case 78 : Process507C(xmlNodeList)
                  Case 79 : Process591(xmlDoc, marketMessage.MarketMessageID)
                  Case 80 : Process594(xmlNodeList)
                  Case 81 : Process595(xmlNodeList)
                  Case 82 : Process596(xmlNodeList)
                  Case 83 : Process597(xmlNodeList)
                  Case 84 : Process598(xmlNodeList)
                  Case 85 : Process601(xmlNodeList)
                  Case 86 : Process602(xmlNodeList)
                  Case 87 : Process700(xmlNodeList)
                  Case 88 : Process700W(xmlNodeList)
                  Case 89 : Process701(xmlNodeList)
                  Case 90 : Process701W(xmlNodeList)
  
              End Select
  
              'Return RedirectToAction("Index")
          Next
          'Return RedirectToAction("Index")
          Return View()
      End If
  
      'Return View(marketmessage)
      Return View()
  End Function
  </code></pre>