title: OPEN XML SDK Infinite Loop
body: <p>I have written some code based on some tutorials to update an existing word document with data from my database tables.</p>
  
  <p>I keep getting the program crashing and advising that I may have an infinite loop, however I am using a for each statement and when I debug it tells me i only have one record.  Cannot understand where the problem is.  </p>
  
  <p>If someone can assist that would be greatly appreciated.  Code below</p>
  
  <p>Controller</p>
  
  <pre><code>    using System.Web.Mvc;
  using Mojito.Models;
  
  
  namespace Mojito.Controllers
  {
      public class KronosDesignDocumentController : Controller
      {
          private MojitoContext _db = new MojitoContext();
          //
          // GET: /KronosDesignDocument/
          public ActionResult Index()
          {
              ViewBag.CustomerId = new SelectList(_db.Customers, "CustomerId", "CustomerName");
              return View();
          }
  
  
          [HttpPost]
          public ActionResult Load(int customerId)
          {
              var kronosDesignTemplate = new KronosDesignDocument.CreateDesignDoc(@"C:\Users\Craig Cocker\Documents\XML Files\Test_Design_Template.docx");
  
              kronosDesignTemplate.CustomerDesignDocument();
  
              ViewBag.CustomerId = new SelectList(_db.Customers, "CustomerId", "CustomerName");
              ViewBag.Message = "Configuration has been loaded successfully";
              return View("Index");
          }
  
  
      }
  }
  </code></pre>
  
  <p>Model</p>
  
  <pre><code>using System.Collections.Generic;
  using DocumentFormat.OpenXml.Packaging;
  using DocumentFormat.OpenXml.Wordprocessing;
  using System.Linq;
  
  
  namespace Mojito.Models
  {
      public class KronosDesignDocument
      {
          public static MojitoContext _db = new MojitoContext();
  
          public class CreateDesignDoc
          {
              private readonly string _xmlPath;
              //private readonly int _customerId;
  
              public CreateDesignDoc(string pathToXmlFiles)
              {
                  _xmlPath = pathToXmlFiles;
              }
  
              public IEnumerable&lt;Customer&gt; CustomerDesignDocument()
              {
  
                  WordprocessingDocument myDoc = WordprocessingDocument.Open(_xmlPath, true);
  
                  var docPart = myDoc.MainDocumentPart;
                  var doc = docPart.Document;
                  var table = new Table();
  
                  var tb = new TopBorder();
                  tb.Val = BorderValues.DashDotStroked;
                  tb.Size = 12;
  
                  var borders = new TableBorders();
                  borders.TopBorder = tb;
  
                  borders.LeftBorder = new LeftBorder() {Val = BorderValues.Single, Size = 12};
                  borders.RightBorder = new RightBorder() {Val = BorderValues.Single};
                  borders.BottomBorder = new BottomBorder() {Val = BorderValues.Single};
                  borders.InsideHorizontalBorder = new InsideHorizontalBorder() {Val = BorderValues.Single};
                  borders.InsideVerticalBorder = new InsideVerticalBorder() {Val = BorderValues.Single};
  
                  var props = new TableProperties();
                  props.Append(borders);
  
                  table.Append(props);
  
                  var customers = _db.Customers.ToList();
                  var customerCollection = new List&lt;Customer&gt;();
  
  
                  foreach (var c in customers)
                  {
                      var tr = new TableRow();
                      var customerName = c.CustomerName;
  
                      var tc = new TableCell();
  
                      var runProp = new RunProperties();
                      runProp.Append(new Bold());
                      runProp.Append(new Color() {Val = "FF0000"});
  
                      var run = new Run();
                      run.Append(runProp);
  
                      var t = new Text(customerName);
                      run.Append(t);
  
                      var justification = new Justification();
                      justification.Val = JustificationValues.Center;
                      var paraProps = new ParagraphProperties(justification);
  
                      var p = new Paragraph();
                      p.Append(paraProps);
                      p.Append(run);
                      tc.Append(p);
  
                      var tcp = new TableCellProperties();
                      var tcw = new TableCellWidth();
                      tcw.Type = TableWidthUnitValues.Dxa;
                      tcw.Width = "2000";
                      tcp.Append(tcw);
                      tcp.Append(tcp);
                      tr.Append(tc);
                      table.Append(tr);
                  }
                  doc.Body.Append(table);
                  doc.Save();
                  return customerCollection;
              }
          }
      }
  }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@model Mojito.Models.KronosDesignDocument
  
  
  @{
      ViewBag.Title = "Kronos Design Document";
  }
  
  &lt;h1&gt;Load Kronos Data&lt;/h1&gt;
  &lt;h2&gt;@ViewBag.Message&lt;/h2&gt;
  
  
  @using (Html.BeginForm("Load", "KronosDesignDocument", FormMethod.Post))
  {
      @Html.AntiForgeryToken()
  
  
      &lt;div class="form-horizontal"&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true)
          &lt;div class="form-group"&gt;
              &lt;div&gt;@Html.Partial("~/Views/Shared/_Customer.cshtml")&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;label for="file"&gt;Filename:&lt;/label&gt;
                  &lt;input type="file" name="file" id="file" /&gt;
                  &lt;input type="submit" value="Create Design Document" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>