Friday, January 3, 2014

Writting and reading xml from Linq.

///writng
STVDataContext stvdb = new STVDataContext(ConfigurationManager.AppSettings["ConnectionString"].ToString());
DataTable dt = (from n in stvdb.tblCustomers select new { n.CustomerId, n.FirstName, n.PostCode, n.CreateDate }).ToADOTable();
new XElement("Employees",
from emp in dt.AsEnumerable()
orderby emp.Field<string>("FirstName") descending
select new XElement("Employee",
new XAttribute("CustomerId", emp.Field<Int64>("CustomerId")),
new XElement("FirstName", emp.Field<string>("FirstName")),
new XElement("PostCode", emp.Field<string>("PostCode")),
new XElement("CreateDate", emp.Field<DateTime>("CreateDate").ToString("MM/dd/yyyy"))
)).Save(("D:\\linqxml.xml"));
///reading
XDocument xdoc = XDocument.Load("D:\\linqxml.xml");
var countires = from country in xdoc.Descendants("Employees").Elements("Employee").Attributes("CustomerId")
select country.Value;

No comments:

Post a Comment