Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7841007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:00:00+00:00 2026-06-02T16:00:00+00:00

even though there are many examples out there, i can’t find what I need?

  • 0

even though there are many examples out there, i can’t find what I need? My question is simply just as the title “How to convert datatable to xml” …

Although, you can suggest solution in any technology, but i have a starting point as well, please have a look at this code.

    Dim xmlDoc As New XDocument(
                    New XElement("Class",                                   
                                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)("DoB")),
                                    New XAttribute("YEAR", row.Field(Of String)("Year")),
                                    New XAttribute("SEX", row.Field(Of String)("Gender")),
                                    New XAttribute("CLASSNAME", row.Field(Of String)("Reg"))
                                )))))

In this example, I need to know how many columns and the name of the columns to do the convertion. I hate to hardcode the columns name into that Linq, is it possible to do in Linq with any number of columns and column names ??

If someone can just suggest me to improve that Linq to handle any columns from the datatable that would be great.

Thanks alot.

LINQ Solution …

    Dim xmlDoc As New XDocument(                       
         From row In dt.Rows
         Select New XElement("Pupil",
         From column In dt.Columns
         Select
         New XAttribute(column.ToString, row.Item(column.ToString))))

For Datatable.WriteXML solution, please refer to Habib’s post

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:00:03+00:00Added an answer on June 2, 2026 at 4:00 pm

    Instead of LINQ you can use: DataTable.WriteXML();

            DataTable dt = new DataTable();
            dt.WriteXml("myxmlfile.xml");
    

    If you want to read and write data according to Schema do the following step:

    • I believe you have data in DataTable, You probably also have an xml for the data, open that XML in visual studio, Generate Schema using -> In the Menu Select -> XML -> Create Schema
    • This will generate a schema file, Save that schema file e.g test1.xsd
    • Try the following code

      DataSet ds = new DataSet();
      ds.ReadXmlSchema("test1.xsd"); //This will read and prepare dataset according to tes1.xsd schema
      ds.ReadXml("test.xml"); // This will read the XML and it should be schema compliant 
      ds.WriteXml("test7.xml");//This will write the new XML according to schema
      

      In your case you can modify the schema to your need and then you can use the XML Schema to generate documents from Dataset to XML. You have to save the datatable in the dataset first. (I am not too sure about this approach, but since I have seen no new replies to this question, I thought it may be helpful to you)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As you can see the upper dark X's are cut even though there is
Is there any possibility of using EF Code First for Oracle DB. Even though
Even though I've been reading other topics about my kind of problem, I can't
This is a pretty common topic and has many answers out there. Situation :
I'm using XPath to pull out just the values of URL128 XML element. There
I went over many examples of this method and still cant figure out whats
There have been many times on StackOverflow where a user asks a question like
Is there a way to click through element, even if it's on top of
Even though it's not part of HTTP 1.1/RFC2616 webapps that wish to force a
Even though it is possible to write generic code in C using void pointer(generic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.