title: Generating dynamic XML in Razor
body: <p>I am using Razor outside of ASP.NET to generate XML documents from a template. I have the following code:</p>
  
  <pre><code>&lt;event&gt;
      &lt;timeouts trigger='@Model.TriggerTimeout' /&gt;
      &lt;parameters&gt;
      @foreach (var parameter in Model.Parameters) {
          &lt;@parameter.Key&gt;@parameter.Value&lt;/@parameter.Key&gt;
      }
      &lt;/parameters&gt;
  &lt;/event&gt;
  </code></pre>
  
  <p>where Model.Parameters is just a name/value collection. My expected output should be:</p>
  
  <pre><code>&lt;event&gt;
      &lt;timeouts trigger='100' /&gt;
      &lt;parameters&gt;
          &lt;name1&gt;value1&lt;/name1&gt;
          &lt;name2&gt;value2&lt;/name2&gt;
          &lt;name3&gt;value3&lt;/name3&gt;
      &lt;/parameters&gt;
  &lt;/event&gt;
  </code></pre>
  
  <p>but what I am actually getting is:</p>
  
  <pre><code>&lt;event&gt;
      &lt;timeouts trigger='100' /&gt;
      &lt;parameters&gt;
          &lt;name1&gt;value1&lt;/@parameter.Key&gt;
          &lt;name2&gt;value2&lt;/@parameter.Key&gt;
          &lt;name3&gt;value3&lt;/@parameter.Key&gt;
      &lt;/parameters&gt;
  &lt;/event&gt;
  </code></pre>
  
  <p>What am I doing wrong? What is the secret sauce that I need to apply to make the dynamic elements close properly?</p>
  
  <p><strong>EDIT</strong>
  So now my model evolves to have multiple events, and my Razor code looks like this:</p>
  
  <pre><code>&lt;events&gt;
  @foreach (var _event in Model.Events) {
      &lt;@_event.Name enabled='@_event.Enabled'&gt;
          &lt;timeouts trigger='@_event.TriggerTimeout' /&gt;
          &lt;parameters&gt;
          @foreach (var parameter in _event.Parameters) {
              @:&lt;@parameter.Key&gt;@parameter.Value&lt;/@(parameter.Key)&gt;
          }
          &lt;/parameters&gt;
      &lt;/@_event.Name&gt;
  }
  &lt;/events&gt;
  </code></pre>
  
  <p>the output should be:</p>
  
  <pre><code>&lt;events&gt;
      &lt;event1 enabled='true'&gt;
          &lt;timeouts trigger='100' /&gt;
          &lt;parameters&gt;
              &lt;name1&gt;value1&lt;/name1&gt;
              &lt;name2&gt;value2&lt;/name2&gt;
          &lt;/parameters&gt;
      &lt;/event1&gt;
      &lt;event2 enabled='false'&gt;
          &lt;timeouts trigger='100' /&gt;
          &lt;parameters&gt;
              &lt;name1&gt;value1&lt;/name1&gt;
          &lt;/parameters&gt;
      &lt;/event2&gt;
  &lt;/events&gt;
  </code></pre>
  
  <p>but it actually is:</p>
  
  <pre><code>&lt;events&gt;
      &lt;event1 enabled='true'&gt;
          &lt;timeouts trigger='100' /&gt;
          &lt;parameters&gt;
              &lt;name1&gt;value1&lt;/name1&gt;
              &lt;name2&gt;value2&lt;/name2&gt;
          &lt;/parameters&gt;
      &lt;/@_event.Name&gt;
      &lt;event2 enabled='false'&gt;
          &lt;timeouts trigger='100' /&gt;
          &lt;parameters&gt;
              &lt;name1&gt;value1&lt;/name1&gt;
          &lt;/parameters&gt;
      &lt;/@_event.Name&gt;
  &lt;/events&gt;
  </code></pre>
  
  <p><strong>SOLUTION</strong>
  Through a <em>lot</em> of trial and error, I eventually tracked down the missing secret sauce. The opening dynamic XML element needed a <code>?:</code> prefix to render the closing element correctly. Most weird.</p>
  
  <pre><code>&lt;events&gt;
  @foreach (var _event in Model.Events) {
      @:&lt;@_event.Name enabled='@_event.Enabled'&gt;
          &lt;timeouts trigger='@_event.TriggerTimeout' /&gt;
          &lt;parameters&gt;
          @foreach (var parameter in _event.Parameters) {
              @:&lt;@parameter.Key&gt;@parameter.Value&lt;/@(parameter.Key)&gt;
          }
          &lt;/parameters&gt;
      &lt;/@_event.Name&gt;
  }
  &lt;/events&gt;
  </code></pre>