avatar
Collection to xml

Guest 102 9th Dec, 2019

CSHARP 3.88 KB
                                           
                         using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;


namespace listtoXml
{

        class StudentDetail
        {
            public int  StudentID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime DOB { get; set; }
            public string Sex { get; set; }
            public int Mark { get; set; }
        }

    class Program
    {
        static void Main(string[] args)
        {
            List<StudentDetail> StdList = new List<StudentDetail>();
            StdList.Add(new StudentDetail() { StudentID = 1, FirstName = "A1", LastName = "A2", DOB = DateTime.Parse("12/11/1971"), Sex = "Male", Mark = 400 });
            StdList.Add(new StudentDetail() { StudentID = 2, FirstName = "B2", LastName = "L2", DOB = DateTime.Parse("01/17/1961"), Sex = "Female", Mark = 485 });
            StdList.Add(new StudentDetail() { StudentID = 3, FirstName = "C3", LastName = "L3", DOB = DateTime.Parse("12/23/1971"), Sex = "Male", Mark = 395 });
            StdList.Add(new StudentDetail() { StudentID = 4, FirstName = "D4", LastName = "L4", DOB = DateTime.Parse("11/15/1976"), Sex = "Female", Mark = 425 });
            StdList.Add(new StudentDetail() { StudentID = 5, FirstName = "E5", LastName = "L5", DOB = DateTime.Parse("05/11/1978"), Sex = "Female", Mark = 410 });
            StdList.Add(new StudentDetail() { StudentID = 6, FirstName = "F6", LastName = "L6", DOB = DateTime.Parse("03/7/1965"), Sex = "Female", Mark = 489 });
            StdList.Add(new StudentDetail() { StudentID = 7, FirstName = "G7", LastName = "L7", DOB = DateTime.Parse("09/11/1972"), Sex = "Male", Mark = 482 });
            StdList.Add(new StudentDetail() { StudentID = 8, FirstName = "H8", LastName = "L8", DOB = DateTime.Parse("12/11/1972"), Sex = "Female", Mark = 450 });
            StdList.Add(new StudentDetail() { StudentID = 9, FirstName = "I9", LastName = "L9", DOB = DateTime.Parse("06/28/1964"), Sex = "Male", Mark = 402 });
            StdList.Add(new StudentDetail() { StudentID = 10, FirstName = "J10", LastName = "L10", DOB = DateTime.Parse("01/11/1978"), Sex = "Male", Mark = 380 });

            try
            {
                         var xEle = new XElement("StudentDetails",
                                    from Std in StdList
                                    select new XElement("StudentDetails",
                                           new XAttribute("StudentID", Std.StudentID),
                                           new XElement("FirstName", Std.FirstName),
                                           new XElement("LastName", Std.LastName),
                                           new XElement("DOB", Std.DOB),
                                           new XElement("Sex", Std.Sex),
                                           new XElement("Mark",Std.Mark)
                                       ));

                xEle.Save(@"D:\dotnet\listtoxml.xml");
                Console.WriteLine("Converted to XML");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }

 }
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes