从Datatable创建XML

时间:2013-08-15 23:26:54

标签: c# asp.net xml linq datatable

我带有以下条目的数据表:

**Companyid   Deptid    Location    Employeeid Employeename    Employeeage**       
001           D001      CA           0001      Jason Bourne     57
001           D001      CA           0002      Will 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

我希望使用Linq在C#中创建XML。 comnpanyid,deptid和location的组合将被视为独特的。这些改变中的任何一个我想要一个新的公司元素创建和所有员工在该公司元素下。 XML应该看起来像

<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>

非常感谢任何帮助。数据表也在companyid,deptid,location

上排序

2 个答案:

答案 0 :(得分:0)

var query =
    from row in table.AsEnumerable()
    group row by new
    {
        CompanyId = row.Field<string>("CompanyId"),
        DeptId = row.Field<string>("DeptId"),
        Location = row.Field<string>("Location")
    }
    into g
    select new XElement("company",
            new XAttribute("CompanyId", g.Key.CompanyId),
            new XAttribute("DeptId", g.Key.DeptId),
            new XAttribute("Location", g.Key.Location),
            from 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));

答案 1 :(得分:0)

var xml = new XElement("companies",
             from e in db.Employees // use your DbSet name here
             group e by new { e.Companyid, e.Deptid, e.Location } into g
             select new XElement("company",
                 new XAttribute("companyid", g.Key.Companyid),
                 new XAttribute("DeptID", g.Key.Deptid),
                 new XAttribute("Location", g.Key.Location),
                 from x in g
                 select new XElement("Employee",
                           new XAttribute("id", x.Employeeid),
                           new XAttribute("Employeename", x.Employeename),
                           new XAttribute("Employeeage", x.Employeeage))
             ));

主要思路很简单 - 当您使用Linq构建xml时,您可以提供任何对象作为元素的内容。因此,我创建了companies根元素并提供了将返回此元素内容的查询。查询按三个字段对实体进行分组,并为每个组创建company元素(用于company元素属性的组密钥)。最后一部分 - 从每个组中选择Employee个元素,并将它们添加到company节点的内容中。