Background
Sometimes we need to work with very complex type dynamic data where data gets stored in a generic list. For example, one customer can purchase multiple types of items, so in this case case the customer is a single entity and the items he is purchasing are different entities having multiple records with dynamic entry where we don't know the count. So in this scenario, it's very difficult to manage the database table structure so we can solve this problem by converting these types of lists into the XML file, and later on we can store the created XML file into a single table and SQL column by defining column data type as XML.
So, let's learn step by step so beginners can also learn how to convert generic lists into XML files in ASP.NET MVC.
Step 1. Create an MVC Application.
Now let us start with a step-by-step approach to the creation of a simple MVC application as in the following.
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project", then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click OK. After clicking, the following window will appear:

Step 2. Create Model Class
Now let us create the model class file named CustomerModel.cs by right-clicking on the model folder as in the following screenshot.

Note. It is not mandatory that Model class should be in the Models folder, it is just for better readability; you can create this class anywhere in the Solution Explorer. This can be done by creating different folder names or without folder names or in a separate class library.
CustomerModel.cs class file code snippet
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ConvertGenericListIntoXMLInMVC.Models
{
public class CustomerorderModel
{
[Display(Name = "Item Code")]
public string ItemCode { get; set; }
[Display(Name = "Product Name")]
public string ProductName { get; set; }
[Display(Name = "Quantity")]
public Int16 Qty { get; set; }
public double Price { get; set; }
[Display(Name = "Total Amount")]
public double TotalAmount { get; set; }
}
public class CustomerOrder
{
// Using CustomerorderModel class as a generic list to add
// multiple orders in list
public List<CustomerorderModel> OrderDetails { get; set; }
}
}
Step 3. Add Controller Class.
Now let us add the MVC 5 controller as in the following screenshot.

After clicking on Add button it will show the window. Specify the Controller name as Customer with the suffix Controller.
Note. The controller name must be having suffix as 'Controller' after specifying the name of controller. Now modify the default code in the CustomerController.cs class file to convert the generic list to XML, after code will look as follows.
CustomerController.cs
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ConvertGenericListIntoXMLInMVC.Models;
using System.Xml.Linq;
namespace ConvertGenericListIntoXMLInMVC.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult AddOrderDetails()
{
// Adding records into list, you can populate this list from database.
List<CustomerorderModel> objOrder = new List<CustomerorderModel>()
{
new CustomerorderModel { ItemCode = "MO12", ProductName = "Mouse", Qty = 1, Price = 150.00, TotalAmount = 150.00 },
new CustomerorderModel { ItemCode = "CO11", ProductName = "Cable", Qty = 2, Price = 250.00, TotalAmount = 500.00 },
new CustomerorderModel { ItemCode = "KO67", ProductName = "KeyBoard", Qty = 3, Price = 500.00, TotalAmount = 1500.00 },
new CustomerorderModel { ItemCode = "PO55", ProductName = "PenDrive", Qty = 1, Price = 200.00, TotalAmount = 200.00 }
};
CustomerOrder ObjOrderDetails = new CustomerOrder();
// Assigning list of records to CustomerOrder
// generic list
ObjOrderDetails.OrderDetails = objOrder;
return View(ObjOrderDetails);
}
[HttpPost]
public ActionResult AddOrderDetails(CustomerOrder Order)
{
// Converting List to XML using LINQ to XML
// the xml doc will get stored into OrderDetails object of XDocument
XDocument OrderDetails = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("CustomerOrder",
from OrderDet in Order.OrderDetails
select new XElement("OrderDetails",
new XElement("ItemCode", OrderDet.ItemCode),
new XElement("ProductName", OrderDet.ProductName),
new XElement("Qty", OrderDet.Qty),
new XElement("Price", OrderDet.Price),
new XElement("TotalAmount", OrderDet.TotalAmount))));
return View();
}
}
}





Vithal WadjePosted Apr 14, 2016, 12:45 PM
Thanks
Vignesh ManiPosted Apr 14, 2016, 8:23 AM
Nice
Vithal WadjePosted Apr 12, 2016, 2:27 PM
Thanks
Debasis SahaPosted Apr 12, 2016, 1:20 PM
Good One..
Kuppurasu NagarajPosted Apr 12, 2016, 12:38 PM
Nice Sharing..
Gowtham KPosted Apr 12, 2016, 12:21 PM
Good One, Thanks for sharing:)
Vithal WadjePosted Apr 12, 2016, 7:58 AM
Thanks Rahul sir
Rahul Kumar SaxenaPosted Apr 12, 2016, 5:54 AM
Generic :) Good work
Vithal WadjePosted Apr 12, 2016, 5:24 AM
Thanks
Jaipal ReddyPosted Apr 12, 2016, 4:39 AM
Nice one sir. .
Manish Kumar ChoudharyPosted Apr 12, 2016, 3:15 AM
Nice one