Following are the setp to read data from XML file using linq query
1st : Add following name spaces by using
using System.Xml;
using System.Xml.Linq;
2nd : Create XDocument object and load XML file
XDocument objXDocument = XDocument.Load(Server.MapPath("~/App_Data/XYZ.xml"));
3rd : Write following linq query to read data from XDcument object
var q = from c in loaded.Descendants("Iteams")
select c.Elements("Item");
You can also specify where condition like following
var q = from c in loaded.Descendants("Items")
where c.Attribute("id").Value = "id1"
select c.Elements("Item");
4th : Check for null and then convert var object 'q' to List
List<XElement> objElem = q.ToList()[0].ToList();
Following is the structuer of the XML file used for this code
<? xml version="1.0" encoding="utf-8" ?>
<Items>
<Item id="id1">
...
Item>
<Item id="id2">
...
Item>
<Item id="id3">
...
Item>
Items>
1st : Add following name spaces by using
using System.Xml;
using System.Xml.Linq;
2nd : Create XDocument object and load XML file
XDocument objXDocument = XDocument.Load(Server.MapPath("~/App_Data/XYZ.xml"));
3rd : Write following linq query to read data from XDcument object
var q = from c in loaded.Descendants("Iteams")
select c.Elements("Item");
You can also specify where condition like following
var q = from c in loaded.Descendants("Items")
where c.Attribute("id").Value = "id1"
select c.Elements("Item");
4th : Check for null and then convert var object 'q' to List
List<XElement> objElem = q.ToList()[0].ToList();
Following is the structuer of the XML file used for this code
<Items>
<Item id="id1">
...
Item>
<Item id="id2">
...
Item>
<Item id="id3">
...
Item>
Items>
Comments
Post a Comment