gpt4 book ai didi

c# - 使用 LINQ 将数据表转为 XML

转载 作者:太空宇宙 更新时间:2023-11-03 20:28:10 25 4
gpt4 key购买 nike

我正在编写一种使用 LINQ 转换数据表的方法。我能够直接从数据表转换为 XML,如下所示:

XDocument doc = new XDocument(new XDeclaration("1.0","UTF-8","yes"),
new XElement("pfolios", from p in dt.AsEnumerable()
select new XElement("pfolio",
new XAttribute("ID", p.ID),
new XAttribute("Date", p.Date),
new XAttribute("Expired", p.Expiry))));

但我需要一些帮助来编写一个方法,该方法将具有任意数量列的数据表作为输入并写入 xml,如下所示: 这个 lamdba 表达式不起作用,但我正在寻找一种方法来简化它。预先感谢您的帮助

  XElement xe = new XElement("pfolios", from p in dt.AsEnumerable()
select new XElement("pfolio",dt.AsEnumerable().ToList().ForEach(dc=> dt.Columns)
new XAttribute(dc.ColumnName, p[dc.ColumnName])));

最佳答案

试试这个

XElement container = new XElement("container");

using (XmlWriter w = container.CreateWriter()) {

DataTable.WriteXml(w, System.Data.XmlWriteMode.WriteSchema, true);
}

关于c# - 使用 LINQ 将数据表转为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com