TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Reading Complex XML Using LINQ
Satyam Sharma
Jun 17
2016
Code
4
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
XML Data
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
company
>
<
Emp_details
>
<
id
>
1
</
id
>
<
emp
>
satyam
</
emp
>
<
emp_type
>
Developer
</
emp_type
>
<
joining_date
>
4/5/2012
</
joining_date
>
<
contact
>
<
phone
>
<
no
>
98924567
</
no
>
<
type
>
home
</
type
>
</
phone
>
<
phone
>
<
no
>
98564786
</
no
>
<
type
>
mobile
</
type
>
</
phone
>
</
contact
>
</
Emp_details
>
<
Emp_details
>
<
id
>
2
</
id
>
<
emp_name
>
anil
</
emp_name
>
<
emp_type
>
Developer
</
emp_type
>
<
joining_date
>
4/5/2012
</
joining_date
>
<
contact
>
<
phone
>
<
no
>
345678
</
no
>
<
type
>
home
</
type
>
</
phone
>
<
phone
>
<
no
>
9856555554786
</
no
>
<
type
>
mobile
</
type
>
</
phone
>
</
contact
>
</
Emp_details
>
<
Emp_details
>
<
id
>
3
</
id
>
<
emp
>
nandan
</
emp
>
<
emp_type
>
Developer
</
emp_type
>
<
joining_date
>
4/5/2012
</
joining_date
>
<
contact
>
<
phone
>
<
no
>
98924568767
</
no
>
<
type
>
home
</
type
>
</
phone
>
<
phone
>
<
no
>
98564234786
</
no
>
<
type
>
mobile
</
type
>
</
phone
>
</
contact
>
</
Emp_details
>
</
company
>
CS File
public
class
Emp
{
public
int
ID
{
get
;
set
;
}
public
string
Emp_Name
{
get
;
set
;
}
public
string
EmpType
{
get
;
set
;
}
public
DateTime join_date
{
get
;
set
;
}
public
Contact Contacts
{
get
;
set
;
}
}
public
class
Contact
{
public
List < PhoneContact > Phone =
new
List < PhoneContact > ();
}
public
class
PhoneContact
{
public
string
Type
{
get
;
set
;
}
public
string
Number
{
get
;
set
;
}
}
// Using LINQ
private
List < Emp > GetempList()
{
XElement xmlDoc = XElement.Load(Server.MapPath(@ "~/Data/Meeting.xml))
var Employee = from cust
in
xmlDoc.Descendants(
"Emp_details"
)
select
new
Emp
{
ID = Convert.ToInt32(cust.Element(
"id"
).Value),
Emp_Name = cust.Element(
"emp_name"
).Value,
EmpType = cust.Element(
"emp_type"
).Value,
join_date = Convert.ToDateTime(cust.Element(
"joining_date"
).Value),
Contacts =
new
Contact()
{
Phone =
new
List < PhoneContact > (from phn
in
cust.Descendants(
"phone"
) select
new
PhoneContact {
Type = phn.Element(
"type"
).Value,
Number = phn.Element(
"no"
).Value
})
}
};
return
Employee.ToList();
}
// Using LAMDA Expression
private
List < Customer > GetempList()
{
XElement xmlDoc = XElement.Load(Server.MapPath(@ "~/Data/Meeting.xml))
var Employee =
xmlDoc.Descendants(
"item"
).Select(cust =>
new
Customer
{
ID = Convert.ToInt32(cust.Element(
"id"
).Value),
Emp_Name = cust.Element(
"emp_name"
).Value,
EmpType = cust.Element(
"emp_type"
).Value,
join_date = Convert.ToDateTime(cust.Element(
"joining_date"
).Value),
Contacts =
new
Contact
{
Phone = cust.Descendants(
"phone"
).Select(phn =>
new
PhoneContact
{
Number = phn.Element(
"type"
).Value,
Type = phn.Element(
"no"
).Value
}).ToList()
}
});
return
Employee.ToList();
}
Complex XML
LINQ