title: The file is corrupt and cannot be opened, C#, XmlWriter
body: <p>Tried to create XML report using XmlWriter.
  Strangely, for large number of records I get the following error when try to open the report file with MS Excel: "The file is corrupt and cannot be opened". However, the report file is not corrupt I think, as with other editors (e.g notepad) I am able to open the file and make sure that the content is OK.</p>
  
  <p>In the code below, the Loop works OK for 1995 upper limit, however it fails when the upper limit is 1996.</p>
  
  <p>Thanks in advance,</p>
  
  <p>========================================================================</p>
  
  <pre><code>    private static void WriteReport(XmlWriter writer, ReportingFilters filters, DataContext context)
      {
  
          writer.WriteStartElement("Table"); // Begin Table
  
          writer.WriteAttributeString("ss", "ExpandedColumnCount", null, "7");
          writer.WriteAttributeString("ss", "ExpandedRowCount", null, "2000");
          writer.WriteAttributeString("x", "FullColumns", null, "1");
          writer.WriteAttributeString("x", "FullRows", null, "1");
  
          writer.WriteStartElement("Column");
          writer.WriteAttributeString("ss", "Width", null, "200");
          writer.WriteEndElement();
  
          writer.WriteStartElement("Column");
          writer.WriteAttributeString("ss", "Width", null, "125");
          writer.WriteEndElement();
  
          writer.WriteStartElement("Column");
          writer.WriteAttributeString("ss", "Width", null, "100");
          writer.WriteEndElement();
  
          writer.WriteStartElement("Column");
          writer.WriteAttributeString("ss", "Width", null, "75");
          writer.WriteEndElement();
  
          writer.WriteStartElement("Column");
          writer.WriteAttributeString("ss", "Width", null, "75");
          writer.WriteEndElement();
  
          // Write the title.
          writer.WriteStartElement("Row");
          writer.WriteStartElement("Cell");
          writer.WriteAttributeString("ss", "StyleID", null, "BorderedTitle");
          writer.WriteStartElement("Data");
          writer.WriteAttributeString("ss", "Type", null, "String");
          writer.WriteString("Selected Dates: " + filters.StartDate.ToShortDateString() + " -" + filters.EndDate.ToShortDateString());
          writer.WriteEndElement();
          writer.WriteEndElement();
          writer.WriteEndElement();
  
          // Write the title.
          writer.WriteStartElement("Row");
          writer.WriteStartElement("Cell");
          writer.WriteAttributeString("ss", "StyleID", null, "BorderedTitle");
          writer.WriteStartElement("Data");
          writer.WriteAttributeString("ss", "Type", null, "String");
          writer.WriteString("Date Report was Run: " + DateTime.Now.ToShortDateString());
          writer.WriteEndElement();
          writer.WriteEndElement();
          writer.WriteEndElement();
  
          //.. etc
  
          var reportResults = ReportingManager.GetHospitalHitRate(context, filters);
          var totalDemos = 0;
          var totalMatched = 0;
  
              // Loop works OK for 1995, 1996 fails with "The file is corrupt and cannot be opened"
              for (int m = 0; m &lt; 1995; m++ )
              {
                  writer.WriteStartElement("Row"); // Begin Content Row
  
                  writer.WriteStartElement("Cell");
                  writer.WriteStartElement("Data");
                  writer.WriteAttributeString("ss", "Type", null, "String");
                  writer.WriteString("aaaaaaaaaaaaaaaaaaaaaaa");
                  writer.WriteEndElement();
                  writer.WriteEndElement();
  
                  writer.WriteStartElement("Cell");
                  writer.WriteStartElement("Data");
                  writer.WriteAttributeString("ss", "Type", null, "String");
                  writer.WriteString("bbbbbbbbbbbbbbb");
                  writer.WriteEndElement();
                  writer.WriteEndElement();
  
                  writer.WriteStartElement("Cell");
                  writer.WriteStartElement("Data");
                  writer.WriteAttributeString("ss", "Type", null, "String");
                  writer.WriteString("cccccccccccc");
                  writer.WriteEndElement();
                  writer.WriteEndElement();
  
                  writer.WriteStartElement("Cell");
                  writer.WriteStartElement("Data");
                  writer.WriteAttributeString("ss", "Type", null, "String");
                  writer.WriteString("dddddddddd");
                  writer.WriteEndElement();
                  writer.WriteEndElement();
  
                  writer.WriteStartElement("Cell");
                  writer.WriteStartElement("Data");
                  writer.WriteAttributeString("ss", "Type", null, "String");
  
                  //.. etc
                  writer.WriteString("%");
                  writer.WriteEndElement();
                  writer.WriteEndElement();
  
                  writer.WriteEndElement();
  
              }
  
          //.. etc
  
          writer.WriteEndElement(); // End Table.
      }
  }
  </code></pre>