Bind XML data to GridView Using Linq to XML

Posted by Vbhujade under C# on 12/2/2012 | Points: 10 | Views : 1748 | Status : [Member] | Replies : 0
public class TitleByApn
private string grantor;
private string grantee;
private string doctype;
private string instrument;
private string month;
private string day;
private string year;
private string remarks;

public string _grantor
{ get { return grantor; }

public string _grantee
get {return grantee ;}

public string _month
get {return month; }

public string _day
get {return day; }

public string _year
get { return year; }

public string _doctype
get{ return doctype; }

public string _remarks
get {return remarks;}

public string _instrument
get {return instrument; }
public static List<TitleByApn> GetTitle()
List<TitleByApn> output = new List<TitleByApn>();
string filepath = @"C:/Title.xml";
XElement xdoc = XElement.Load(filepath);
var authorList = xdoc.Descendants("title_doc").Select(x => x.Value).ToList();
foreach (string item in authorList)
string Month = (from o in xdoc.Descendants("title_doc").Descendants("recorded")
select o.Element("month").Value).First();
string Day = (from o in xdoc.Descendants("title_doc").Descendants("recorded")
select o.Element("day").Value).First();
string Year = (from o in xdoc.Descendants("title_doc").Descendants("recorded")
select o.Element("year").Value).First();
string granTor = (from o in xdoc.Descendants("title_doc").Descendants("party_info").Descendants("party")
where o.Attribute("role").Value == "GRANTOR"
select o.Element("name").Value).FirstOrDefault();
string granTee = (from o in xdoc.Descendants("title_doc").Descendants("party_info").Descendants("party")
where o.Attribute("role").Value == "GRANTEE"
select o.Element("name").Value).First();
string Doctype =(from o in xdoc.Descendants("title_doc").Descendants("inst")
select o.Element("type").Value).First();
string instNo = (from o in xdoc.Descendants("title_doc").Descendants("inst")
select o.Element("number").Value).First();
string REmarks = (from o in xdoc.Descendants("title_doc").Descendants("remarks")
select o.Element("text").Value).First();
output.Add(new TitleByApn(granTor, granTee, Month, Day, Year,instNo, Doctype, REmarks));
return output;
This is the code responsible to bind the class to gridview:
GridView1.DataSource = TitleByApn.GetTitle();

I know because of FirstOrDefault() only first record is displaying in gridview.
I want code to display all the records in gridview.
Please help me ASAP.
Thanks in advance.


(No response found.)

Login to post response