你的位置:首页 > ASP.net教程

[ASP.net教程]ListT保存为XML文件

今天我们学习怎样把List<T>写成一个
为了解决一个问题,我们得需先创建一个自定义的Result()在ASP.NET MVC中。这个Result操作可以继承ActionResult或者ContentResult。

 

代码:

 class  : ActionResult  {    public T Data { private get; set; }    public string FilePhysicalPath { private get; set; }    public override void ExecuteResult(ControllerContext context)    {      = new typeof(T));      using (StreamWriter writer = new StreamWriter(FilePhysicalPath))      {        

View Code


创建Model:

 

创建一个Entity,是数据的集合:

 

code source:

 public class NumberEntity  {    public List<Number> Numbers()    {      return new List<Number>(){        new Number() { Num = 20 },        new Number() { Num = 40 },        new Number() { Num = 60 },        new Number() { Num = 80 }      };    }  }

View Code


接下来,创建控制器


code source:

 public ActionResult return View();    }    public ActionResult ConvertListSaveFile()    {      NumberEntity ne = new NumberEntity();      string fileName = Guid.NewGuid().ToString() + ".";      string filephysicalPath = Server.MapPath("~/Temp/") + fileName;      return new ()      {        Data = ne.Numbers(),        FilePhysicalPath = filephysicalPath      };    }

View Code


完成视图,简单:

 效果演示: