我之前有一个表单,它输入了关于两个人的关系标题和FirstName、LastName的信息。一旦输入信息并单击Submit,就会生成一个xml,看起来像:
<?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>
型号-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; }
}
}
View.cshtml是对FormModel中的Relation类强类型的。它包含文本框和一个提交按钮,该按钮调用控制器中的CreateData操作。
<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-读取传入的数据并创建使用辅助类XmlResult序列化的XmlModel对象。
[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
};
}
助手类
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);
}
}
}
因此,这会在浏览器上显示一个对话框,提供保存或打开xml的选项
现在,我们希望表单上有更多的输入,并且基于这些输入,需要在同一个xml文件中生成一些更复杂的xml数据。
xml格式很复杂,但它将是以前xml中的新数据,如:
<?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>
正如您所看到的,表单可以提供多个Relation项以及一些ComplexData。我不想为这个ComplexData结构创建类,所以我可能想要一些直接编写xml的方法,而不是序列化对象。同样,将视图与关系模型联系起来现在也没有意义了。
那么,我该怎么做,这样我仍然能够在浏览器中生成xml文件呢?在当前的解决方案中,它是在服务器端发布,但我愿意将其更改为完全在客户端工作。不确定是否可以使用jquery/javascript完成此操作?
您询问是否可以100%在客户端创建XML?对您不能将其作为文件下载(出于安全考虑),但可以将其放在文本区域。
您可以通过在客户端连接一个字符串来生成xml。
以下是一篇文章,展示了如何仅在客户端使用不同的javascript技术处理XML:使用JavaScript 在内存中生成XML文档
Jquery:如何使用jQuery解析XML?
aaand以下是允许在客户端下载的一些技巧,其中一些不需要服务器端代码:使用HTML5/Javascript生成并保存文件