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 8820909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:43:29+00:00 2026-06-14T05:43:29+00:00

Possible Duplicate: Alternative ways to convert data table to customized XML DataTable dt =

  • 0

Possible Duplicate:
Alternative ways to convert data table to customized XML

DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Product_ID", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("Product_Name", Type.GetType("System.String")));
dt.Columns.Add(new DataColumn("product_Price", Type.GetType("System.Int32")));
fillRows("hello1", dt, "product1", 1111);
fillRows("hello2", dt, "product2", 2222);
fillRows("hello3", dt, "product3", 3333);
fillRows("hello4", dt, "product4", 4444);


var xmlColumnZero = dt.AsEnumerable().Select(col => col[0].ToString()).ToArray() ; // row 0 turnovermultiplieer
var xmlRowZero = dt.Columns;
string firstColumnHeader = dt.Columns[0].ToString();
// XmlDocument xmldoc = new XmlDocument();
XmlWriter writer = XmlWriter.Create("employees.xml");
writer.WriteStartDocument();
writer.WriteStartElement("Employees");

// XmlElement first = xmldoc.CreateElement("Order");
//xmldoc.AppendChild(first);
foreach (DataColumn dc  in dt.Columns ) 
{

    if (dc.ToString() == firstColumnHeader) continue;
    string firstcol = dc.ToString();
    writer.WriteStartElement(firstcol);
    // XmlElement colRoot = xmldoc.CreateElement(firstcol);
    //first.AppendChild(colRoot);
    for (int i = 0 ; i <dt.Rows.Count && i< xmlColumnZero.Length ; i++)
    {
        string firstrow = xmlColumnZero[i];
        string dtagaga = dt.Rows[i][dc].ToString();
        writer.WriteElementString(firstrow, dtagaga);
       // XmlElement rowRoot = xmldoc.CreateElement(firstrow, dtagaga);
        //colRoot.AppendChild(rowRoot);


    }
    writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();

I want the XMl to be stored into a string while i am creating XMLWriter.

Is there another way i can create xml out of the table

XML should look like

The xml writer method stores everything into an xml file in the program location. Would prefer a string to be saved

<Employees>
<Product_Name>
<hello1>product1</hello1>
hello2>product2</hello2>
hello3>product3</hello3>
hello4>product4</hello4>
</product_name>
<product_Price>
<hello1>1111</hello1>
hello2>2222</hello2>
hello3>3333</hello3>
hello4>4444</hello4>
</product_Price>
</Employees>
  • 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-14T05:43:30+00:00Added an answer on June 14, 2026 at 5:43 am

    Just use overloaded method XmlWriter.Create(StringBuilder output) to create xml string. In this case all output will be written to StringBuilder instead of file:

    StringBuilder builder = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(builder);
    //... build xml here
    
    string xml = builder.ToString();
    

    Also you can write xml to MemoryStream with XmlWriter.Create(Stream output).

    Stream stream = new MemoryStream();
    XmlWriter writer = XmlWriter.Create(stream);
    // ... build xml here
    
    stream.Position = 0;
    string xml = new StreamReader(stream).ReadToEnd();
    

    UPDATE

    Extension method below will generate your xml string. By default it uses first column as element names, but you can pass any column index for column with meta data. Also I use table name to generate “Employees” tag, so provide name when you create data table DataTable dt = new DataTable("Employees");.

    public static string ToXml(this DataTable table, int metaIndex = 0)
    {
        XDocument xdoc = new XDocument(
            new XElement(table.TableName,
                from column in table.Columns.Cast<DataColumn>()
                where column != table.Columns[metaIndex]
                select new XElement(column.ColumnName,
                    from row in table.AsEnumerable()
                    select new XElement(row.Field<string>(metaIndex), row[column])
                    )
                )
            );
    
        return xdoc.ToString();
    }
    

    Usage is very simple:

    string xml = dt.ToXml();
    

    Output:

    <Employees>
      <Product_Name>
        <hello1>product1</hello1>
        <hello2>product2</hello2>
        <hello3>product3</hello3>
        <hello4>product4</hello4>
      </Product_Name>
      <product_Price>
        <hello1>111</hello1>
        <hello2>222</hello2>
        <hello3>333</hello3>
        <hello4>444</hello4>
      </product_Price>
    </Employees>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Alternative to itoa() for converting integer to string C++? How to convert
Possible Duplicate: PHP split alternative? // Successful geocode $geocode_pending = false; $coordinates = $xml->Response->Placemark->Point->coordinates;
Possible Duplicate: How are SSL certificate server names resolved/Can I add alternative names using
Possible Duplicate: C# - Is there a better alternative than this to ‘switch on
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Is there an alternative to Dictionary/SortedList that allows duplicates? I am looking
Possible Duplicate: CURL alternative in Python I'm trying to write a python snippet that
Possible Duplicate: I need a good way to get data from a thread to
Possible Duplicate: Jmeter alternative Other than JMeter, whether there are any open source tool
Possible Duplicate: C# - Is there a better alternative than this to 'switch on

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.