title: Read xml file from end to start c#
body: <p>I have big xml files which have many data (unnecessary for me now) and a very large number of procedures. I want to read the xml from the end to take the last procedure.
  (From the last START PROCEDURE to the last END PROCEDURE and everything between them)</p>
  
  <p>I tried from the start but it is not efficient because it takes too long to get through all the data, and taking each procedure as last until the next is found.</p>
  
  <p>I also tried to read line-by-line storing in an array and with a for loop i started searching from the end but this takes ages too.</p>
  
  <p>The getElementByTagName is also not useful because I have many start and end procedures and not very efficient if i must go through all of them.</p>
  
  <p>The xml files have the following format:
  <code>
      .............
      .............
      .............
  </code>
  <code>
      &lt;value&gt;
          &lt;struct&gt; 
              &lt;member&gt; 
                  &lt;name&gt; procedureAction &lt;/name&gt;
                  &lt;value&gt; 0 &lt;/value&gt;
              &lt;/member&gt;
              &lt;member&gt; 
                  &lt;name&gt; mainType &lt;/name&gt; 
                  &lt;value&gt; 200 &lt;/value&gt;
              &lt;/member&gt;
              &lt;member&gt; 
                  &lt;name&gt; subType &lt;/name&gt; 
                  &lt;value&gt; 30 &lt;/value&gt; 
              &lt;/member&gt;
              &lt;member&gt; 
                  &lt;name&gt; time &lt;/name&gt; 
                  &lt;value&gt; 1890 &lt;/value&gt; 
                 &lt;/member&gt;
          &lt;/struct&gt; 
      &lt;/value&gt;
  </code></p>
  
  <p><code>
      .................
      .................
      .................
      ..................
  </code>
  <code>
      &lt;value&gt; 
          &lt;struct&gt; 
              &lt;member&gt; 
                  &lt;name&gt; procedureAction &lt;/name&gt; 
                  &lt;value&gt; 1 &lt;/value&gt; &lt;/member&gt;
              &lt;member&gt; 
                  &lt;name&gt; mainType &lt;/name&gt; 
                  &lt;value&gt; 200 &lt;/value&gt; 
              &lt;/member&gt;
              &lt;member&gt; 
                  &lt;name&gt; subType &lt;/name&gt; 
                  &lt;value&gt; 30 &lt;/value&gt; 
              &lt;/member&gt;
              &lt;member&gt; 
                  &lt;name&gt; time &lt;/name&gt; 
                  &lt;value&gt; 1890 &lt;/value&gt; 
              &lt;/member&gt;
          &lt;/struct&gt; 
      &lt;/value&gt;
  </code>
  <code>
      .............
      .............
      .............
  </code>
  The procedureAction will value 0 is a START,
  the procedureAction will value 1 is an END.</p>
  
  <p>How can I read the xml file from the end?
  The iterator does not work because it needs fixed-size encoding.</p>
  
  <p>Thank you in advance.</p>
  
  <p>The procedureSearch function splits the xml when the "procedureAction" element name is found and saves the piece in a position in the array.
  this is done for all lines that contain procedureAction.
  I tried to search for the last "procedureAction value 0" in the array but takes too much time
  `
      private string[] procedureSearch(string inputXmlAsString){</p>
  
  <pre><code>    string[] returnValue = null;
      string[] split;
      string startSplit = "&lt;value&gt;&lt;struct&gt;&lt;member&gt;&lt;name&gt;procedureAction&lt;/name&gt;&lt;value&gt;";
      string stopSplit = "&lt;/value&gt;&lt;/member&gt;&lt;/struct&gt;&lt;/value&gt;&lt;value&gt;";
      const int MAX_COUNT = 100000;
      string[] allResults = new string[MAX_COUNT];
      int allResultsCounter = 0;
  
      while (inputXmlAsString.CompareTo("") == 1) { 
          if (inputXmlAsString.Contains(startSplit)) {
  
              //if the text is contained in the remaining log...
              //split at the start (throw the split[0], keep split[1])
              split = inputXmlAsString.Split(new string[] { startSplit }, StringSplitOptions.RemoveEmptyEntries);
              string[] tempSplitter = null;
              //gather the remaining
              inputXmlAsString = string.Join(startSplit, split.Skip(1).ToList());
              //and split the remaining at the stop
              tempSplitter = inputXmlAsString.Split(new string[] { stopSplit }, StringSplitOptions.RemoveEmptyEntries);
              //connect from the start to the stop
              allResults[allResultsCounter] = startSplit + tempSplitter[0];
              string tempComp = stopSplit;
              tempSplitter = tempComp.Split(new String[] { "&lt;value" }, StringSplitOptions.RemoveEmptyEntries);
              allResults[allResultsCounter] += tempSplitter[0];
              allResultsCounter++;
  
          } else {
              inputXmlAsString = "";
          }//if substring exists
          //Console.WriteLine("ReadLog after if: "+ readLog);
  
      }//while readLog not null
  
      returnValue = new string[allResultsCounter];
      for (int b = 0; b &lt; allResultsCounter; b++) {
          returnValue[b] = allResults[b];
      }//for
      return returnValue;
  }//end of procedureSearch function`
  </code></pre>
  
  <p>The main is very simple:</p>
  
  <p><code>string pieceOfXml = System.IO.File.ReadAllText(logPath);
      const int MAX = 100000;
      string[] allProcedures = new string[MAX];
      int allProceduresCounter = 0;
      allProcedures = procedureSearch(pieceOfXml);</code></p>
  
  <p>The result is an array with almost 3000 lines: An example of a piece of a line</p>
  
  <p><code>&lt;value&gt;&lt;struct&gt;&lt;member&gt;&lt;name&gt;procedureAction&lt;/name&gt;&lt;value&gt;20&lt;/value&gt;&lt;/member&gt;
      &lt;member&gt;&lt;value&gt;&lt;struct&gt;&lt;member&gt;&lt;name&gt;mainType&lt;/name&gt;&lt;value&gt;31&lt;/value&gt;&lt;/member&gt;
      &lt;member&gt;&lt;name&gt;subType&lt;/name&gt;&lt;value&gt;0&lt;/value&gt;&lt;/member&gt;&lt;/struct&gt;&lt;/value&gt;&lt;/member&gt;
      &lt;member&gt;&lt;name&gt;time&lt;/name&gt;&lt;value&gt;97&lt;/value&gt;&lt;/member&gt;&lt;/member&gt;&lt;/struct&gt;&lt;/value&gt;</code></p>