C# Tutorials and offshore development in India
Tutorials Resources Forum Reviews Communities Interview Jobs Projects Training Your Ad Here


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » General »

Reading XML File Using Linq


Posted Date: 01 Oct 2009    Resource Type: Code Snippets    Category: General
Author: YuvarajMember Level: Gold    
Rating: 1 out of 5Points: 7



This code shows how to read an XML file using LINQ

 
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = ReadXML();
GridView1.DataBind();
}
public List ReadXML()
{
string fileName = Server.MapPath("XMLFile.xml");
XDocument doc = XDocument.Load(fileName);

var EpDetail = doc.Descendants("Employee").Select(a =>
new Employee
{
Name = a.Element("Name").Value,
Id = a.Element("id").Value,
Age = a.Element("Age").Value,
Salary = a.Element("Salary").Value,
Address = a.Descendants("Address")
.Select(b =>
new Address()
{
Country = b.Element("Country").Value,
State = b.Element("State").Value,

}).FirstOrDefault()
}).ToList();
return EpDetail;
}

public class Address
{
public string Country
{
set;
get;
}
public string State
{
set;
get;
}
}
public class Employee
{
public string Name
{
set;
get;
}
public string Id
{
set;
get;
}
public string Age
{
set;
get;
}
public string Salary
{
set;
get;
}
public Address Address
{
set;
get;
}
}


//Sample XML File is attached with this Snippets

Attachments

  • Sample XML File (33354-1754-XMLFile.xml)


  • Responses to the resource: "Reading XML File Using Linq"

    No responses found. Be the first to respond and make money from revenue sharing program.

    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Read XML Using LINQ  .  XML using LINQ  .  XML + LINQ  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Event Logger
    Previous Resource: Typing Contents in Ms Word from DotNet Applications
    Return to Discussion Resource Index
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources




    About Us    Contact Us    Privacy Policy    Terms Of Use