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
AttachmentsSample XML File (33354-1754-XMLFile.xml)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|