从xml输出模型以查看MVC4

时间:2013-07-06 00:14:19

标签: c# asp.net-mvc-4

在我学习MVC / C#时,将xml数据序列化到模型然后将其(绑定)到视图中的最有效方法是什么?

我有以下

 public static MovieSummary Deserialize()
 {
     XmlSerializer serializer = new XmlSerializer(typeof(MovieSummary));
     TextReader textReader;

     textReader = new StreamReader("c:\\movies.xml");

     MovieSummary summary = (MovieSummary)serializer.Deserialize(textReader); 
     textReader.Close();
     return summary;
 }

public class MovieSummary
{
    public List<Movie> Movies { get; set; }
}

public class Movie
{
    public int id { get; set; }
    public string name { get; set; }
}


 <?xml version="1.0" encoding="utf-8"?>
 <movies>
      <movie>
          <id>1</id>
          <name>The Dark Knight</name>
      </movie>
      <movie>
          <id>2</id>
          <name>Iron Man</name>
      </movie>
  </movies>

我想调用deserialize函数并使用摘要。控制器的代码如何查找公共ActionResult ListMovies()?

1 个答案:

答案 0 :(得分:1)

调用您的函数,然后将结果返回到您的视图:

public ActionResult ListMovies()
{
  MovieSummary summary = Deserialize();
  return View(summary);
}

在您的视图中,您将引用模型并生成HTML。