c# - Creating XML from Datatable -
this question has answer here:
- how convert datatable xml? 1 answer
i hava datatable following entries:
**companyid deptid location employeeid employeename employeeage** 001 d001 ca 0001 jason bourne 57 001 d001 ca 0002 smith 45 001 d001 nv 0003 kurt rusell 47 002 d002 ca 0008 panda 57 002 d002 ca 0009 fox 45 002 d002 nv 0010 wolf 35
i want xml created in c# using linq. combination of comnpanyid,deptid , location treated unique. 1 of change want new company elemnt created , employee under company element. xml should like
<companies> <company companyid="001" deptid="d001" location="ca"> <employee id="0001" employeename="jason bourne" employeeage=57/> <employee id="0002" employeename="will smith" employeeage=45/> </company> <company companyid="001" deptid="d001" location="nv"> <employee id="0003" employeename="kurt rusell" employeeage=47/> </company> <company companyid="002" deptid="d002" location="ca"> <employee id="0008" employeename="panda" employeeage=57/> <employee id="0009" employeename="fox" employeeage=45/> </company> <company companyid="002" deptid="d002" location="nv"> <employee id="0010" employeename="wolf" employeeage=35/> </company> </companies>
any appreciated. datatable sorted on companyid,deptid,location
var query = row in table.asenumerable() group row new { companyid = row.field<string>("companyid"), deptid = row.field<string>("deptid"), location = row.field<string>("location") } g select new xelement("company", new xattribute("companyid", g.key.companyid), new xattribute("deptid", g.key.deptid), new xattribute("location", g.key.location), row in g select new xelement( "employee", new xattribute("id", row.field<string>("id")), new xattribute("employeename", row.field<string>("employeename")), new xattribute("employeeage", row.field<string>("employeeage")))); var document = new xdocument(new xelement("companies", query));
Comments
Post a Comment