Reputation: 59
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:
<?xml version="1.0" encoding="utf-16"?>
<Data>
<RelationData>
<Relation Title="Friends">
<Persons>
<Person FirstName="Tom" LastName="B" />
<Person FirstName="Jerry" LastName="C" />
</Persons>
</Relation>
</RelationData>
</Data>
Models - FormModel.cs
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; }
}
XmlModel.cs
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; }
}
}
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.
<div class="form-horizontal">
@Html.ValidationSummary(true)
<div class="form-group">
<input id="CreateData" type="submit" class="btn btn-primary" value="Create Data File" />
</div>
</div>
Controller.cs - Reads the data passed in and creates the XmlModel object that is serialized using helper class XmlResult.
[HttpPost]
public ActionResult CreateData(Relation rData)
{
var xmlModelData = new XmlModel();
// Create elements in XmlModel object using values in rData
...
return new XmlResult<XmlModel>()
{
Data = xmlModelData
};
}
Helper class
public class XmlResult<T> : 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);
}
}
}
So this gives a dialog on the browser giving an option to save the xml or open it.
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.
The xml format is complex but it will be new data in the previous xml like:
<?xml version="1.0" encoding="utf-16"?>
<Data>
<RelationData>
<Relation Title="Friends">
<Persons>
<Person FirstName="Tom" LastName="B" />
<Person FirstName="Jerry" LastName="C" />
</Persons>
</Relation>
<Relation Title="Friends">
<Persons>
<Person FirstName="Hello" LastName="AD" />
<Person FirstName="World" LastName="BC" />
</Persons>
</Relation>
...
</RelationData>
<ComplexData>
...
</ComplexData>
</Data>
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.
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?
Upvotes: 3
Views: 12450
Reputation: 36
You ask if you can create the XML 100% on the client side? Yes. You cannot download it as a file (due to security) but you could put it in a text area.
You can make xml just by concatenating a string on the client side.
Here is a post that shows how to work with XML with different javascript techniques on the client side only: Generate XML document in-memory with JavaScript
Jquery: How to parse XML using jQuery?
aaaand here are some tricks for allowing downloads on the client side, some of which require no server side code: Using HTML5/Javascript to generate and save a file
Upvotes: 1