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

  • Home
  • SEARCH
  • 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 6696865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:23:31+00:00 2026-05-26T06:23:31+00:00

How to convert XML string to DataTable in C#? I tried the following code:

  • 0

How to convert XML string to DataTable in C#?

I tried the following code:

public DataTable stam()
{
    string xmlData = "<Names><Name>a</Name><Name>b</Name><Name>c</Name><Name>d</Name></Names>";

    XElement x = XElement.Parse(xmlData);

    DataTable dt = new DataTable();

    XElement setup = (from p in x.Descendants() select p).First();

    foreach (XElement xe in setup.Descendants()) // build your DataTable
        dt.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt

    var all = from p in x.Descendants(setup.Name.ToString()) select p;

    foreach (XElement xe in all)
    {
        DataRow dr = dt.NewRow();
        foreach (XElement xe2 in xe.Descendants())
            dr[xe2.Name.ToString()] = xe2.Value; //add in the values
        dt.Rows.Add(dr);
    }

    return dt;
}

and it returns an empty DataTable.

  • 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-05-26T06:23:31+00:00Added an answer on May 26, 2026 at 6:23 am
    public DataTable stam()    
    {
        StringReader theReader = new StringReader(xmlData);
        DataSet theDataSet = new DataSet();
        theDataSet.ReadXml(theReader);
    
        return theDataSet.Tables[0];
    }
    

    You can use a StringReader to load it into a DataSet. From there, the table with the first index will contain the DataTable.

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

Sidebar

Related Questions

string xml=< theme> <colors> <color> <code>1</code> <name>blue</name> <priority>5</priority> </color> <color> <code>2</code> <name>red</name> <priority>2</priority> </color>
Whats wrong with this code? it suppose to convert xml data to string and
I have an xml document object that I need to convert into a string.
Is there any PHP script to convert XML file to DTD. I tried a
I am trying to convert an Xml String to XML DOM object using Xerces
I'm looking for a function to convert a string to the xml string with
Following my question on serializing a System.Array to a Xml string, I would like
How do I convert XML to string and get the element value? Example XML
I am trying to convert an XML string into and array. SimpleXmlIterator only accepts
I'm trying to convert a hash to a XML-string with this module XML::Hash::LX but

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.