Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Showing Images Using XML in MVC 4

Article:
Viewed:  2343 
Posted On:  25/01/2014 12:43:12 
How can show Images using XML in MVC 4 application ? 

In this article I am going to show how we can show Images using XML in MVC 4.

I am going to create a new Project:

1 Showing Images Uisng XML in MVC 4.png

Image 1.


2 Showing Images Uisng XML in MVC 4.png

Image 2.

Now I add a New folder MyAlbum  and I add some images like below:

3 Showing Images Uisng XML in MVC 4.png

Image 3.

Now Right Click on Solution Explorer -> Add New Item -> Choose a XML

4 Showing Images Uisng XML in MVC 4.png

Image 4.

My ImageInformation.xml is:

<?xml version="1.0" encoding="utf-8" ?>
<images>
  <image>
    <filename>ICC.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>Kapil.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>MSD.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>MSD2.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>MSD3.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>MSD4.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>Raina.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>Sachin.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>Team.jpg</filename>
    <description>Indian Team</description>
  </image>
  <image>
    <filename>Team2.jpg</filename>
    <description>Indian Team</description>
  </image>
</images>
 

Now Right Click on Model Folder -> Add New Item -> Add New Class -> ImageListing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace Showing_Images_Using_XML_in_MVC_4.Models
{
    public class ImageListing
    {
        public ImageListing(string path, string desc)
        {
            Path = path;
            Description = desc;
        }
 
        public string Path { get; set; }
        public string Description { get; set;}
    }
}

Now Right Click on Model Folder -> Add New Item -> Add New Class -> ImageModel

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
 
namespace Showing_Images_Using_XML_in_MVC_4.Models
{
    public class ImageModel : List<ImageListing>
    {
        public ImageModel()
        {
            string directoryOfImage = HttpContext.Current.Server.MapPath("~");
            XDocument imageData = XDocument.Load(directoryOfImage + @"/ImageInformation.xml");
            var images = from image in imageData.Descendants("image") select new ImageListing(image.Element("filename").Value, image.Element("description").Value);
            this.AddRange(images.ToList<ImageListing>());
        }
    }
}

Now Right Click on Controller -> Add New Controller

5 Showing Images Uisng XML in MVC 4.png

Image 5.

6 Showing Images Uisng XML in MVC 4.png

Image 6.

Now ImageDirectoryController is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Showing_Images_Using_XML_in_MVC_4.Models;
 
namespace Showing_Images_Using_XML_in_MVC_4.Controllers
{
    public class ImageDirectoryController : Controller
    {
        //
        // GET: /ImageDirectory/
 
        public ActionResult Index()
        {
            return View(new ImageModel());
        }
 
    }
}
 
Now Right Click on Index -> Add New View

7 Showing Images Uisng XML in MVC 4.png

Image 7.

Do below code in your View:

@model IEnumerable<Showing_Images_Using_XML_in_MVC_4.Models.ImageListing>
@{
    ViewBag.Title = "Showing Image Using XML in MVC 4";
}
@foreach (var item in Model)
{
    <img src="@Url.Content("~/MyAlbum/")@item.Path" height="100"  />
}

 
Now Run your application:

8 Showing Images Uisng XML in MVC 4.png

Image 8.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us