Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于动态coumns的数据表到XML_C#_Linq - Fatal编程技术网

C# 基于动态coumns的数据表到XML

C# 基于动态coumns的数据表到XML,c#,linq,C#,Linq,我正在尝试将数据表转换为XML。 上面的链接提到了如何对具有已知列的表执行此操作。当列是动态的(不是LINQ中的硬编码)时,有没有办法做到这一点 string s= new XElement("Employees", from empList in dt.AsEnumerable() orderby empList.Field<decimal>("ESalary") descending

我正在尝试将数据表转换为XML。

上面的链接提到了如何对具有已知列的表执行此操作。当列是动态的(不是LINQ中的硬编码)时,有没有办法做到这一点

 string s=  new XElement("Employees",
                 from empList in dt.AsEnumerable()
                 orderby empList.Field<decimal>("ESalary") descending
                 select new XElement("Employee",
                      new XAttribute("EmployeeId", empList.Field<Int32>("EID")),
                      new XAttribute("Salary", empList.Field<decimal>("ESalary")),
                      new XElement("EmployeeName", empList.Field<string>("EName")),
                      new XElement("Designation", empList.Field<string>("EDesignation"))
                 )).ToString() 
            }
string s=new-XElement(“员工”,
来自dt.AsEnumerable()中的empList
orderby empList.Field(“ESalary”)降序
选择新XElement(“员工”,
新XAttribute(“EmployeeId”,employist.Field(“EID”),
新XAttribute(“工资”,雇员字段(“ESalary”),
新XElement(“EmployeeName”,employist.Field(“EName”),
新XElement(“指定”,雇员字段(“电子指定”))
)).ToString()
}
这里Emp、EmpID、salary等都是硬编码的。我想让它适用于所有的表。LINQ有办法吗

我试着说:

   XElement cust = new XElement("TEST",
        from str in table.AsEnumerable()
        let fields = (from dr in table.Rows.Cast<DataRow>()
                         select dr.ItemArray)
        select new XElement("Client", splitTempalte.Zip(fields, (name, value) => new XElement(name, value)))
    );
XElement cust=新XElement(“测试”,
来自表中的str.AsEnumerable()
let fields=(来自表.Rows.Cast()中的dr)
选择dr.ItemArray)
选择NewXelement(“客户端”,splitTempalte.Zip(字段,(名称,值)=>NewXelement(名称,值)))
);
如果没有选项,那么我将不得不为每一行使用for循环,在这个循环中,为每一列使用另一个for循环,并使用LINQ zip。只是想看看是否有比使用datatable to LINQ(使用动态列)更好的方法