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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:35:06+00:00 2026-05-12T20:35:06+00:00

I have a DataTable which I select from database (Well, these data cross several

  • 0

I have a DataTable which I select from database (Well, these data cross several tables, after the query and putting into a DataTable, it shows at below)

ColumnA ColumnB
a             11
b             33
b             44
a             22
b             55

but I want to transform it into an XML like this

<root>
   <header name ='a'>
       <item name='11' />
       <item name='22' />
   </header>
   <header name ='b'>
       <item name='33' />
       <item name='44' />
       <item name='55' />
   </header>

</root>

Is there an easy way to implement it by C#?

  • 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-12T20:35:06+00:00Added an answer on May 12, 2026 at 8:35 pm

    OK, second approach after learning that the data is available in a DataTable to begin with.

    The code is a bit more involved, since based on a DataTable, you can’t really do much in terms of grouping etc. I am building up the XmlDocument (since you’re on .NET 2.0) while scanning through the rows of data. I need to keep track of the <header> elements in a dictionary, in order to add a second, third entry with the same “ColumnA” value to that already existing XmlElement in the document – it’s a bit involved, but if you study it carefully, I hope you see it’s really no trickery or anything – just a bit of bookkeeping along the way of building the XmlDocument:

    // create the XmlDocument and add <root> node
    XmlDocument doc = new XmlDocument();
    doc.AppendChild(doc.CreateElement("root"));
    
    // dictionary to keep track of <header> nodes
    Dictionary<string, XmlNode> nodesPerColumnA = new Dictionary<string, XmlNode>();
    
    // Loop through data rows
    foreach (DataRow row in tbl.Rows)
    {
       // extract values for ColumnA and ColumnB as strings
       string columnAValue = row["ColumnA"].ToString();
       string columnBValue = row["ColumnB"].ToString();
    
       // create a new <item> XmlNode and fill its attribute @Name 
       XmlElement newNode = doc.CreateElement("item");
    
       XmlAttribute newNodeAttribute = doc.CreateAttribute("name");
       newNodeAttribute.InnerText = columnBValue;
    
       newNode.Attributes.Append(newNodeAttribute);
    
       // check if we already have a <header> node for that "ColumnA" value
       if(nodesPerColumnA.ContainsKey(columnAValue))
       {
           // if so - just add <item> below that <header>
           XmlNode parent = nodesPerColumnA[columnAValue];
    
           parent.AppendChild(newNode);
       }
       else
       {
           // if not - create appropriate <header> node and its @name attribute
           XmlElement header = doc.CreateElement("header");
    
           XmlAttribute headerAttr = doc.CreateAttribute("name");
           headerAttr.InnerText = columnAValue;
    
           header.Attributes.Append(headerAttr);
    
           header.AppendChild(newNode);
    
           doc.DocumentElement.AppendChild(header);
    
           // store that <header> xmlnode into the dictionary for future use
           nodesPerColumnA.Add(columnAValue, header);
        }
     }
    
     // check the contents of the XmlDocument at the end
     string xmlContents = doc.InnerXml;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 216k
  • Answers 216k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you defined a function both that looked like this:… May 12, 2026 at 11:08 pm
  • Editorial Team
    Editorial Team added an answer You can do this: cp `cat list.txt` new-folder/ Note that… May 12, 2026 at 11:08 pm
  • Editorial Team
    Editorial Team added an answer The class you're looking for (e.g. the one with main()… May 12, 2026 at 11:08 pm

Related Questions

I have a database checkpoint in QTP (HP QuickTest Pro); this checkpoint works if
I'm creating a dynamic gridview function which will bind different tables from DB into
I have a crystal report that is connecting to a database. The datatable from
In a .NET Datatable, the columns are Object types, which can include a datatable

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.