Saturday, 8 August 2015

XML File Generation And Data Writing Using C#.Net In Asp.Net MVC

8/08/2015 - By Pranav Singh 0

This article will show you how you can perform xml file generation and data writing using c#.net in asp.net mvc.





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>

Now we have done run  the application to check the output.



Here is the final output.

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top