title: Generate xml file using data in ASP.NET MVC form
body: <p>I had a form earlier which took input about the Relation title and FirstName, LastName of 2 persons. Once entered the info and clicks Submit, an xml got generated which looked like: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;Data&gt;
    &lt;RelationData&gt;
      &lt;Relation Title="Friends"&gt;
        &lt;Persons&gt;
          &lt;Person FirstName="Tom" LastName="B" /&gt;
          &lt;Person FirstName="Jerry" LastName="C" /&gt;
        &lt;/Persons&gt;
      &lt;/Relation&gt;
    &lt;/RelationData&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>Models - FormModel.cs</p>
  
  <pre><code>public class Relation
  {
      public string Title { get; set; }
      public string Person1FirstName { get; set; }
      public string Person1LastName { get; set; }
      public string Person2FirstName { get; set; }
      public string Person2LastName { get; set; }
  }
  </code></pre>
  
  <p>XmlModel.cs</p>
  
  <pre><code>public class XmlModel
  {
      [XmlRoot("Data")]
      public class Data
      {
          [XmlElement("RelationData")]
          public RelationData RelationData { get; set; }
      }
  
      [SerializableAttribute()]
      public class RelationData
      {
          [XmlElement("Relation")]
          public Relation Relation { get; set; }
      }
  
  [SerializableAttribute()]
      public class Relation
      {
          [XmlElementAttribute()]
          public Persons Persons { get; set; }
  
          [XmlAttributeAttribute()]
          public string Title { get; set; }
      }
  
  [SerializableAttribute()]
      public class Persons
      {
          [XmlElementAttribute("Person")]
          public Person[] Person { get; set; }
      }
  
  [SerializableAttribute()]
      public class Person
      {
          [XmlAttributeAttribute()]
          public string FirstName { get; set; }
  
          [XmlAttributeAttribute()]
          public string LastName { get; set; }
      }
  }
  </code></pre>
  
  <p>The View.cshtml is strongly typed to the Relation class in FormModel. It contains the text boxes and a submit button which calls the CreateData action in the controller.</p>
  
  <pre><code>&lt;div class="form-horizontal"&gt;
      @Html.ValidationSummary(true)
      &lt;div class="form-group"&gt;
          &lt;input id="CreateData" type="submit" class="btn btn-primary" value="Create Data File" /&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Controller.cs - Reads the data passed in and creates the XmlModel object that is serialized using helper class XmlResult.</p>
  
  <pre><code>[HttpPost]
  public ActionResult CreateData(Relation rData)
  {
  
      var xmlModelData = new XmlModel();
  
      // Create elements in XmlModel object using values in rData
      ...
  
      return new XmlResult&lt;XmlModel&gt;()
      {
          Data = xmlModelData
      };
  }
  </code></pre>
  
  <p>Helper class</p>
  
  <pre><code>public class XmlResult&lt;T&gt; : ActionResult
  {
      public T Data { private get; set; }
  
      public override void ExecuteResult(ControllerContext context)
      {
          context.HttpContext.Response.Clear();
          context.HttpContext.Response.ContentType = "text/xml";
          context.HttpContext.Response.ContentEncoding = Encoding.UTF8;
  
          var filename = DateTime.Now.ToString("mmddyyyyhhss") + ".xml";
          context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
  
          using(var writer = new StringWriter())
          {
              XmlSerializer xs = new XmlSerializer(typeof(T));
              xs.Serialize(writer, Data);
              context.HttpContext.Response.Write(writer);
          }
      }
  }
  </code></pre>
  
  <p>So this gives a dialog on the browser giving an option to save the xml or open it. </p>
  
  <p>Now we want some more inputs on the form and based on these inputs some more complex xml data needs to be generated in the same xml file.</p>
  
  <p>The xml format is complex but it will be new data in the previous xml like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;Data&gt;
    &lt;RelationData&gt;
      &lt;Relation Title="Friends"&gt;
        &lt;Persons&gt;
          &lt;Person FirstName="Tom" LastName="B" /&gt;
          &lt;Person FirstName="Jerry" LastName="C" /&gt;
        &lt;/Persons&gt;
      &lt;/Relation&gt;
      &lt;Relation Title="Friends"&gt;
        &lt;Persons&gt;
          &lt;Person FirstName="Hello" LastName="AD" /&gt;
          &lt;Person FirstName="World" LastName="BC" /&gt;
        &lt;/Persons&gt;
      &lt;/Relation&gt;
      ...
    &lt;/RelationData&gt;
    &lt;ComplexData&gt;
     ...
    &lt;/ComplexData&gt;
  &lt;/Data&gt;
  </code></pre>
  
  <p>As you can see, the form can supply multiple Relation items as well as some ComplexData. I don't want to create classes for this ComplexData structure, so I probably want some way to directly write the xml instead of serializing the objects. Also tying the view to Relation model doesn't make sense now.</p>
  
  <p>So how can I go about this, so that I am still able to generate the xml file in the browser? In the current solution, it is doing a server post but I am willing to change that to entirely work on the client side. Not sure if this can be done using jquery/javascript?</p>