title: Merging huge (2GB) XMLs in memory (without any memory exceptions)
body: <p>I would like a C# code that optimally appends 2 XML strings.  Both of them are of same schema.  I tried <code>StreamReader / StreamWriter; File.WriteAllText; FileStream</code>
  The problem I see is, it uses more than 98% of physical memory thus results in out of memory exception.  </p>
  
  <p>Is there a way to optimally merge without getting any memory exceptions? Time is not a concern for me.</p>
  
  <p>If making it available in memory is going to be a problem, then what else could be better? Saving it on File system?</p>
  
  <p><strong>Further Details:</strong>
  Here is my simple program: to provide better detail</p>
  
  <pre><code>static void Main(string[] args)
          {
              Program p = new Program();
              XmlDocument x1 = new XmlDocument();
              XmlDocument x2 = new XmlDocument();
              x1.Load("C:\\XMLFiles\\1.xml");
              x2.Load("C:\\XMLFiles\\2.xml");
              List&lt;string&gt; files = new List&lt;string&gt;();
              files.Add("C:\\XMLFiles\\1.xml");
              files.Add("C:\\XMLFiles\\2.xml");
              p.ConsolidateFiles(files, "C:\\XMLFiles\\Result.xml");
              p.MergeFiles("C:\\XMLFiles\\Result.xml", x1.OuterXml, x2.OuterXml, "&lt;Data&gt;", "&lt;/Data&gt;");
              Console.ReadLine();
  
          }
  
          public void ConsolidateFiles(List&lt;String&gt; files, string outputFile)
          {
              var output = new StreamWriter(File.Open(outputFile, FileMode.Create));
              output.WriteLine("&lt;Data&gt;");
              foreach (var file in files)
              {
                  var input = new StreamReader(File.Open(file, FileMode.Open));
                  string line;
                  while (!input.EndOfStream)
                  {
                      line = input.ReadLine();
                      if (!line.Contains("&lt;Data&gt;") &amp;&amp;
                          !line.Contains("&lt;/Data&gt;"))
                      {
                          output.Write(line);
                      }
                  }
              }
              output.WriteLine("&lt;/Data&gt;");
          }
          public void MergeFiles(string outputPath, string xmlState, string xmlFederal, string prefix, string suffix)
          {
              File.WriteAllText(outputPath, prefix);
              File.AppendAllText(outputPath, xmlState);
              File.AppendAllText(outputPath, xmlFederal);
              File.AppendAllText(outputPath, suffix);
          }
  </code></pre>
  
  <p><strong>XML Sample:</strong> 
  <code>&lt;Data&gt; &lt;/Data&gt;</code> is appended at the beginning  &amp; End</p>
  
  <p>XML 1: <code>&lt;Sections&gt; &lt;Section&gt;&lt;/Section&gt; &lt;/Sections&gt;</code></p>
  
  <p>XML 2: <code>&lt;Sections&gt; &lt;Section&gt;&lt;/Section&gt; &lt;/Sections&gt;</code></p>
  
  <p>Merged: <code>&lt;Data&gt; &lt;Sections&gt; &lt;Section&gt;&lt;/Section&gt; &lt;/Sections&gt; &lt;Sections&gt; &lt;Section&gt;&lt;/Section&gt; &lt;/Sections&gt; &lt;/Data&gt;</code> </p>