Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 2 - Creating an XML document using in-memory collection of objects

Suggested Videos
Part 1 - LINQ to XML



In Part 1, we discussed creating an XML document by hard-coding the XML elements in code. In this video we will discuss, creating an XML document using in-memory collection of objects.



In real time applications we usually have the data residing in a database table. There are many data access technologies available to retrieve data from the database. For example we could use 
1. Entity Framework or
2. LINQ to SQL or
3. ADO.NET

Once the data is retrieved from the database, the application may store the data in in-memory data structures like arrays, list, queue etc. So, now let us discuss creating an XML document from an array of Student objects. Here are the steps.

Step 1 : Create a new Console Application. Name it Demo.

Step 2 : Add a class file to the project. Name it Student.cs. Copy and paste the following code.
namespace Demo
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int TotalMarks { get; set; }

        public static Student[] GetAllStudents()
        {
            Student[] students = new Student[4];

            students[0] = new Student { Id = 101, Name = "Mark",
                                        Gender = "Male", TotalMarks = 800 };
            students[1] = new Student { Id = 102, Name = "Rosy",
                                        Gender = "Female", TotalMarks = 900 };
            students[2] = new Student { Id = 103, Name = "Pam",
                                        Gender = "Female", TotalMarks = 850 };
            students[3] = new Student { Id = 104, Name = "John",
                                        Gender = "Male", TotalMarks = 950 };

            return students;
        }
    }
}

Step 3 : Copy and paste the following code in Program.cs file.
using System.Linq;
using System.Xml.Linq;

namespace Demo
{
    class Program
    {
        public static void Main()
        {
            XDocument xmlDocument = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),

                new XComment("Creating an XML Tree using LINQ to XML"),

                new XElement("Students",

                    from student in Student.GetAllStudents()
                    select new XElement("Student", new XAttribute("Id", student.Id),
                                new XElement("Name", student.Name),
                                new XElement("Gender", student.Gender),
                                new XElement("TotalMarks", student.TotalMarks))
                            ));

            xmlDocument.Save(@"C:\Demo\Demo\Data.xml");
        }
    }
}

Upon running the console application, an XML file with name Data.xml should be created in the respective project folder. To see the xml file, click on "Show All Files" icon in the solution explorer. Double click on the xml file to open it in visual studio.

LINQ to XML tutorial

1 comment:

It would be great if you can help share these free resources