Reputation: 7823
..
I need help converting datatable to xml using Linq. I could do it with hardcoded column names as you can see in my code .. but i need it without hardcoding it ... hope could someone point me out how to do that .. thanks a alot
Example datatable ..
My linq query ..
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"))
))
Upvotes: 0
Views: 1609
Reputation: 102743
Why not just load the columns from the row the same way you loaded the rows from the table?
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))
)
)
Upvotes: 3