Page Nav

SHOW

Grid

GRID_STYLE

Hover Effects

TRUE

Pages

Latest Tech Updates

latest

Write data in XML file from an object and Read the same data From XML file using C#.

  1. Write and Read data from XML file using System; using System.Collections.Generic; using System.Linq; using System.Text; using System....

 

1.Write and Read data from XML file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace BackToBasic
{
    class XMLToObject
    {
        //TODO : Add Your Code Here

        //Getting the XML data from local folder

        static string path = @"C:\Data\SampleXMLData.xml";

        public static void Main()
        {
            try
            {
                //Create XML file from object
                Student objStudent = new Student();
                Console.WriteLine("Enter Student ID.");
                objStudent.ID = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Student Name.");
                objStudent.Name = Console.ReadLine().ToString();
                Console.WriteLine("Enter Student Registration Number.");
                objStudent.RegNo = Console.ReadLine().ToString();
                Console.WriteLine("Enter Student Address.");
                objStudent.Address = Console.ReadLine().ToString();
                Console.WriteLine("Enter Student Total Mark.");
                objStudent.TotalMark = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter Studnet Standard.");
                objStudent.Class = Console.ReadLine().ToString();
                Console.WriteLine("Enter Student Grade.");
                objStudent.Grade = Console.ReadLine().ToString();
                Console.WriteLine("Enter Student Contact.");
                objStudent.ContactNumber = Console.ReadLine().ToString();

                int result = CreateXMLFile(objStudent);
                if (result == 0)
                {
                    Console.WriteLine("Wrting Data to XML File failled,Please try again");
                }
                else
                {
                    //Read the XML file which we created now
                    string xmlString = System.IO.File.ReadAllText(path);

                    //Read all XML data and store inside object.
                    Student objstd = (Student)ConvertXMLToObject(xmlString, typeof(Student));

                    //Read all data from object
                    Console.WriteLine();
                    Console.WriteLine("After Successfully Read data from XML file below are the data;");
                    Console.WriteLine();
                    Console.WriteLine("StudnetID : {0}", objstd.ID);
                    Console.WriteLine("Student Name : {0}", objstd.Name);
                    Console.WriteLine("Student Registration Number : {0}", objstd.RegNo);
                    Console.WriteLine("Student Address : {0}", objstd.Address);
                    Console.WriteLine("Studenet Total Mark : {0}", objstd.TotalMark);
                    Console.WriteLine("Student Standard : {0}", objstd.Class);
                    Console.WriteLine("student Grade : {0}", objstd.Grade);
                    Console.WriteLine("Student Contact : {0}", objstd.ContactNumber);
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static Object ConvertXMLToObject(string xml, Type classType)
        {
            StringReader stringReader = null;
            XmlSerializer xmlSerializer = null;
            XmlTextReader xmltextReader = null;
            Object obj = null;
            try
            {
                stringReader = new StringReader(xml);
                xmlSerializer = new XmlSerializer(classType);
                xmltextReader = new XmlTextReader(stringReader);
                obj = xmlSerializer.Deserialize(xmltextReader);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return obj;
        }
        public static int CreateXMLFile(Student student)
        {
            try
            {
                
                System.Xml.Serialization.XmlSerializer writer =
                    new System.Xml.Serialization.XmlSerializer(typeof(Student));

                var path = @"C:\Data\SampleXMLData.xml";
                System.IO.FileStream file = System.IO.File.Create(path);
                writer.Serialize(file, student);
                file.Close();
                return 1;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
        }
    }
[DataContract(Name ="Studnet")]
   public class Student
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string RegNo { get; set; }
        [DataMember]
        public string Address { get; set; }

        [DataMember]
        public int TotalMark { get; set; }

        [DataMember]

        public string Class { get; set; }

        [DataMember]
        public string Grade { get; set; }
        [DataMember]
        public string ContactNumber { get; set; }
    }
}