0

..

我需要使用 Linq 将数据表转换为 xml 的帮助。正如您在我的代码中看到的那样,我可以使用硬编码的列名来做到这一点..但我需要它而不需要硬编码...希望有人能指出我该怎么做..非常感谢

示例数据表..

在此处输入图像描述

我的 linq 查询..

    Dim xmlDoc As New XDocument(                           
             From row In dt.AsEnumerable()
                From row In dt.AsEnumerable()
                    Select New XElement("PUPIL",
                    New XAttribute("FIRSTNAME", row.Field(Of String)("First Name")),
                    New XAttribute("LASTNAME", row.Field(Of String)("Last Name")),
                    New XAttribute("DOB", row.Field(Of String)("Date of Birth")),
                    New XAttribute("Gender", row.Field(Of String)("Gender")),
                    New XAttribute("City", row.Field(Of String)("City"))
              ))
4

1 回答 1

3

为什么不按照从表中加载行的方式从行中加载列?

Dim xmlDoc As New XDocument(
    From row In dt.Rows
        Select XElement("PUPIL",
            From column In dt.Columns
                Select
                    New XAttribute(column.Name, row.Item(column.Name))
         )
)
于 2012-04-24T09:12:10.753 回答