XML File
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<VehVendorAvails>
<VehVendorAvail RPH="1">
<VehAvailCore>
<RentalRate AvailabilityStatus="S" RateCode="SRULES" STM_RatePlan="D">
<Vehicle>
<VehType>MCAR</VehType>
</Vehicle>
</RentalRate>
<VehicleCharges>
<VehicleCharge Amount="99.49" CurrencyCode="USD" GuaranteeInd="G">
<AdditionalDayHour>
<Day CurrencyCode="USD" />
<Hour CurrencyCode="USD" MileageAllowance="UNL" Rate="49.74" />
</AdditionalDayHour>
<Mileage Allowance="UNL" CurrencyCode="USD" ExtraMileageCharge=".00" UnitOfMeasure="MI" />
<SpecialEquipTotalCharge CurrencyCode="USD" />
<TotalCharge Amount="545.46" CurrencyCode="USD" />
</VehicleCharge>
</VehicleCharges>
</VehAvailCore>
<Vendor Code="ZE" CompanyShortName="HERTZ" ParticipationLevel="B" />
</VehVendorAvail>
<VehVendorAvail RPH="2">
<VehAvailCore>
<RentalRate AvailabilityStatus="S" RateCode="APH28" STM_RatePlan="D">
<Vehicle>
<VehType>ECAR</VehType>
</Vehicle>
</RentalRate>
<VehicleCharges>
<VehicleCharge Amount="25.00" CurrencyCode="USD" GuaranteeInd="Q">
<AdditionalDayHour>
<Day CurrencyCode="USD" />
<Hour CurrencyCode="USD" MileageAllowance="UNL" Rate="12.50" />
</AdditionalDayHour>
<Mileage Allowance="UNL" CurrencyCode="USD" ExtraMileageCharge=".00" UnitOfMeasure="MI" />
<SpecialEquipTotalCharge CurrencyCode="USD" />
<TotalCharge Amount="164.76" CurrencyCode="USD" />
</VehicleCharge>
</VehicleCharges>
</VehAvailCore>
<Vendor Code="ZE" CompanyShortName="HERTZ" ParticipationLevel="B" />
</VehVendorAvail>
</VehVendorAvails>
</catalog> Source Code using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml;
namespace XMLToLinq
{
class Program
{
static void Main(string[] args)
{
List < Item > objListItem = new List < Item > ();
XElement xmlElement = XElement.Load(@ "D:\\Data1.xml");
XNamespace nm = "Xmlnm";
#region Fetch All
var lstVehVendorAvail = xmlElement.Descendants(nm + "VehVendorAvail");
foreach(var vendorItem in lstVehVendorAvail)
{
Item objItem = new Item();
objItem.Key = vendorItem.Name.LocalName;
objItem.Value = vendorItem.Value;
objItem.ListItems = new List < Item > ();
var item = vendorItem.Descendants();
objItem.ListItems.Add(new Item
{
Key = item.Attributes("AvailabilityStatus").FirstOrDefault().Name.LocalName, Value = item.Attributes("AvailabilityStatus").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = item.Attributes("RateCode").FirstOrDefault().Name.LocalName, Value = item.Attributes("RateCode").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = item.Attributes("STM_RatePlan").FirstOrDefault().Name.LocalName, Value = item.Attributes("STM_RatePlan").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = item.Elements(nm + "VehType").FirstOrDefault().Name.LocalName, Value = item.Elements(nm + "VehType").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "VehicleCharge_Amount", Value = item.Attributes("Amount").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "VehicleCharge_CurrencyCode", Value = item.Attributes("CurrencyCode").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "GuaranteeInd", Value = item.Attributes("GuaranteeInd").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "Day_CurrencyCode", Value = item.Attributes("CurrencyCode").Skip(1).Take(1).FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "Hour_CurrencyCode", Value = item.Attributes("CurrencyCode").Skip(2).Take(1).FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "MileageAllowance", Value = item.Attributes("MileageAllowance").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "Rate", Value = item.Attributes("Rate").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "Allowance", Value = item.Attributes("Allowance").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "Mileage_CurrencyCode", Value = item.Attributes("CurrencyCode").Skip(3).Take(1).FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "ExtraMileageCharge", Value = item.Attributes("ExtraMileageCharge").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "UnitOfMeasure", Value = item.Attributes("UnitOfMeasure").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "SpecialEquipTotalCharge_CurrencyCode", Value = item.Attributes("CurrencyCode").Skip(4).Take(1).FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "TotalCharge_Amount", Value = item.Attributes("Amount").Skip(1).Take(1).FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "TotalCharge_CurrencyCode", Value = item.Attributes("CurrencyCode").Skip(5).Take(1).FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "Code", Value = item.Attributes("Code").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "CompanyShortName", Value = item.Attributes("CompanyShortName").FirstOrDefault().Value
});
objItem.ListItems.Add(new Item
{
Key = "ParticipationLevel", Value = item.Attributes("ParticipationLevel").FirstOrDefault().Value
});
objListItem.Add(objItem);
}
Console.ReadKey(true);
#endregion
}
class Item
{
public List < Item > ListItems
{
get;
set;
}
public string Key
{
get;
set;
}
public string Value
{
get;
set;
}
}
}
}