简体   繁体   中英

output model from xml to view MVC4

learning MVC/C# as I go, what's the most efficient way of serializing xml data to a model and then presenting (bind) that into a view?

I have the following

 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>

I'd like to call the deserialize function and consume the summary. how would the code for the controller look for public ActionResult ListMovies()?

Call your function, then return the results to your view:

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

Inside your view, you would reference your model and generate your HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM