How to read xml using Linq
-> 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.