Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Reading Complex XML Using LINQ
WhatsApp
Satyam Sharma
Jun 17
2016
4.1
k
0
0
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
Up Next
Reading Complex XML Using LINQ