C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » XML »

How to read xml using Linq


Posted Date: 29 Oct 2009    Resource Type: Articles    Category: XML
Author: vipulMember Level: Diamond    
Rating: 1 out of 5Points: 25 (Rs 10)



-> XMl is widly used in all the programming language. So we can used XML Passed that data,store the data and many more operation. so we
can read and write that xml.

-> So before .net 3.0 we can used XMLDocument ,XMLRead and other object we can used for reading and wirting xml. But after .net 3.0 and
more version we can easily read and write the xml like any class object. We can access xml element like porperty of the obhect.

-> Here i can explain some example how can read the xml and how to access their element using Linq.

-> For XML File : Employee.xml (Attached)

-> You can read the file using XDocument and XElement ( you can also refer
http://stackoverflow.com/questions/1542073/xdocument-or-xmldocument site for more detail)

1) Read XML :

using XDocument you can read xml this way


XDocument xDoc = XDocument.Load(Server.MapPath("Employees.xml"));

var name = from nm in xDoc.Root.Elements("Employee") select nm;

foreach (XElement xEle in name)
{
Response.Write(xEle);
}


using XElement you can read xml this way


XElement xelement = XElement.Load(Server.MapPath("Employees.xml"));

var name = from nm in xelement.Elements("Employee")
select nm;

foreach (XElement xEle in name)
{
Response.Write(xEle);
}



-> Here you can see the difference is that using XDocument you can get element by xDoc.Root.Elements("Employee") which is begin with
root and when you can used xElement then you can direct give the Elemnet tag name xelement.Elements("Employee").

-> For all following example i used XElement for reading XML.

2) Get Data from perticular element ( Search perticular record ,order by) :

-> If you need only name from the xml then you can used this way

 
XElement xelement = XElement.Load(Server.MapPath("Employees.xml"));

var name = from nm in xelement.Elements("Employee") select nm.Elements("Name");


foreach (XElement xEle in name)
{
Response.Write(xEle);

}


-> If yoy want only zip code from the xml which is sub elemtn of the Address then you can used this way


XElement xelement = XElement.Load(Server.MapPath("Employees.xml"));

var name = from nm in xelement.Elements("Employee") select nm.Elements("Address").Element("Zip");

OR

var name = from nm in xelement.Elements("Employee") let zips = nm.Elements("Address").Element("Zip") select zips;


foreach (XElement xEle in name)
{
Response.Write(xEle);

}


-> If you want name in the asceding or descing order you ca nused this way


XElement xelement = XElement.Load(Server.MapPath("Employees.xml"));

var name = from nm in xelement.Elements("Employee")
orderby (string)nm.Element("Address").Element("Zip") descending
select nm;

foreach (XElement xEle in name)
{
Response.Write(xEle.Element("Address").Element("Zip").Value);
;
}


Here is can used xEle.Element("Address").Element("Zip").Value . If you
want perticular valu from the give n result you ca nused this way.

-> If you search some record in the xml file you can used this way


XElement xelement = XElement.Load(Server.MapPath("Employees.xml"));

var name = from nm in xelement.Elements()
where (string)nm.Element("Sex") == "Male"
select nm;

// Only Male Records come
foreach (XElement xEle in name)
{
Response.Write(xEle); // Here you get full xml element of the male persone
//Response.Write(xEle.Element("Name")); // Here you get only name of the male persone

}


This is not end it is hust begining.
If i am wrong any where please infom me so i can change that way.



Attachments

  • Employee.xml (34477-29217-Employees.xml)
  • Employee.xml (34477-29219-Employees.xml)


  • Responses


    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.
    XML  .  LINQ  .  Asp.net 3.0  .  

    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: How to Build Your First Business Website
    Previous Resource: What is XML?
    Return to Discussion Resource Index
    Post New Resource
    Category: XML


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use