XML File :
<Employee>
<Employee Ecode="001" Ename="EEEE" />
<Employee Ecode="001" Ename="CCCC" />
<Employee Ecode="001" Ename="AAAA" />
<Employee Ecode="001" Ename="DDDD" />
<Employee Ecode="001" Ename="BBBB" />
</Employee>
Below is the C# Code in Linq
static void Main(string[] args)
{
XElement xelement = XElement.Load("..\\..\\Employee.xml");
var dictNm = (from element in xelement.Descendants("Employee")
let Epname = (string)element.Attribute("Ename")
orderby Epname
select new
{
EmpID = element.Attribute("Ecode").Value,
EmpName = Epname
})
.ToDictionary(c => c.EmpID , c => c.EmpName);
foreach (var item in dictNm)
{
Console.WriteLine(item.Value);
}
Console.ReadLine();
}
Output will be :
AAAA
BBBB
CCCC
DDDD
EEEE