This article will show you how you can perform xml file
generation and data writing using c#.net in asp.net mvc.
Some of my previous articles are as follows: How
To Read XML File In DataSet And Display in DataGridview Using C#.Net, Read
XML File in DataTable and Bind to DataList In Asp.Net Using C#.Net, How
To Read XML File in DataTable Using C#.Net In Asp.Net MVC, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net MVC Using
C#.Net, Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net.
So for this article first we will create a new asp.net mvc application and add a folder in which we will we will add the xml file.
So for this article first we will create a new asp.net mvc application and add a folder in which we will we will add the xml file.
Now we will add the model class file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace XMLReadeinMVC.Models
{
public class StudentModel
{
public
int Id { get;
set; }
public
string Name { get;
set; }
public
string Address { get;
set; }
}
}
|
After creating model class file we will add the controller
class file.
using System.Web.Mvc;
using System.Data;
using System.Xml;
using System.Collections.Generic;
using XMLReadeinMVC.Models;
namespace XMLReadeinMVC.Controllers
{
public class HomeController
: Controller
{
//
// GET:
/Home/
public
ActionResult Index()
{
List<StudentModel> objStudentModel = new List<StudentModel>();
string
xmlFilePath = @"XMLFile\StudentsXML.xml";
objStudentModel =
GetStudentData();
CreateXMLFile(xmlFilePath,
objStudentModel);
ViewBag.Message = "XML file created successfully.";
return
View();
}
/// <summary>
/// Method to create xml file
/// </summary>
/// <param
name="xmlFilePath"></param>
public
void CreateXMLFile(string
xmlFilePath, List<StudentModel> objStudentModel)
{
using
(XmlWriter writer = XmlWriter.Create(Server.MapPath(xmlFilePath)))
{
writer.WriteStartDocument();
writer.WriteStartElement("Students");
foreach
(StudentModel item in objStudentModel)
{
writer.WriteStartElement("Student");
writer.WriteElementString("id",
item.Id.ToString());
writer.WriteElementString("name",
item.Name);
writer.WriteElementString("address", item.Address);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
/// <summary>
/// Method to generate xml data
/// you can fatch data dynamically and pass it to collection
/// </summary>
/// <returns></returns>
public
List<StudentModel>
GetStudentData()
{
List<StudentModel> objStudentModel = new List<StudentModel>();
objStudentModel.Add(new StudentModel
{ Id = 1, Name = "Name 1",
Address = "Address 1" });
objStudentModel.Add(new StudentModel
{ Id = 2, Name = "Name 2",
Address = "Address 2" });
objStudentModel.Add(new StudentModel
{ Id = 3, Name = "Name 3",
Address = "Address 3" });
objStudentModel.Add(new StudentModel
{ Id = 4, Name = "Name 4",
Address = "Address 4" });
return
objStudentModel;
}
}
}
|
In above code I have create a function to provide the data. XmlWriter
to write and create the xml file. After this we will create the view.
@{
ViewBag.Title = "XML
File Generation And Data Writing Using C#.Net In Asp.Net MVC";
}
<h2>
@ViewBag.Message
</h2>
|
0 comments:
Please let me know your view