totalnoob
totalnoob

Reputation: 2741

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()?

Upvotes: 0

Views: 333

Answers (1)

Matt Houser
Matt Houser

Reputation: 36073

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.

Upvotes: 1

Related Questions