C Sharp Example 1: How to load XML file in LIST object in C#.Net

C Sharp Example 1: How to load XML file in LIST object in C#.Net

Using this tutorial we will learn to load XML file in the LIST object in C#.NET.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LoadXMLFileInList
{
            public class Order
        {
            public int OrderID { get; set; }
            public DateTime OrderDate { get; set; }
            public decimal Total { get; set; }
        }

        public class Customer
        {
            public string CustomerID { get; set; }
            public string CompanyName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string Region { get; set; }
            public string PostalCode { get; set; }
            public string Country { get; set; }
            public string Phone { get; set; }
            public string Fax { get; set; }
            public Order[] Orders { get; set; }
        }

    class Program
    {
        public static List<Customer> customerList;
        static void Main(string[] args)
        {
            customerList = (
                    from e in XDocument.Load("C:\\Users\\nrang\\Desktop\\Personal\\dotnet examples\\Csharp Examples\\LoadXMLFileInList\\LoadXMLFileInList\\Customers.xml").
                              Root.Elements("customer")
                    select new Customer
                    {
                        CustomerID = (string)e.Element("id"),
                        CompanyName = (string)e.Element("name"),
                        Address = (string)e.Element("address"),
                        City = (string)e.Element("city"),
                        Region = (string)e.Element("region"),
                        PostalCode = (string)e.Element("postalcode"),
                        Country = (string)e.Element("country"),
                        Phone = (string)e.Element("phone"),
                        Fax = (string)e.Element("fax"),
                        Orders = (
                            from o in e.Elements("orders").Elements("order")
                            select new Order
                            {
                                OrderID = (int)o.Element("id"),
                                OrderDate = (DateTime)o.Element("orderdate"),
                                Total = (decimal)o.Element("total")
                            })
                            .ToArray()
                    })
                    .ToList();

            foreach (var city in customerList)
                Console.WriteLine(city.City);

            Console.Read();

        }
    }
}

         
Download Source Code:
https://drive.google.com/open?id=0Bzkzt4QtX66pemt6V2Y3WG1reFE&authuser=0

OutPut:
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar