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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:24:18+00:00 2026-05-25T23:24:18+00:00

When I read an xml file into a dataset, if the root node has

  • 0

When I read an xml file into a dataset, if the root node has more than one of each child tag, it doesn’t create a table for the root node. Why is this? Is there any way to get it to generate the root table without modifying the xml?

static void Main(string[] args) {
    var ds =Load(@"<root>
      <Stores>Lambton Quay</Stores>
      <Stores>Willis Street</Stores>
    </root>");
    var ds2 = Load(@"<root>
      <t>1</t>
      <Stores>Lambton Quay</Stores>
      <Stores>Willis Street</Stores>
    </root>");
    Console.WriteLine("ds table count: {0}", ds.Tables.Count); //1 <- expecting 2
    Console.WriteLine("ds2 table count: {0}", ds2.Tables.Count); //2
    Console.ReadKey();
}
static DataSet Load(string xml) {
    var xd = new XmlDocument();
    xd.LoadXml(xml);
    var ds = new DataSet();
    ds.ReadXml(new XmlNodeReader(xd));
    return ds;
}

Edit:

To be clear I want to know why the DataSet ds doesn’t have a root table while ds2 does.

Someone made the decisions:

  1. if only 1 node exists with a given tag name under a given parent tag, it becomes a column in the table named by the parent tag.
  2. if more than one such node exists, it becomes a table on its own
  3. if all child nodes appear more than once and the parent node is the document root, the parent table does not get created

What is the reason for #3?

  • 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-25T23:24:18+00:00Added an answer on May 25, 2026 at 11:24 pm

    The first XML example doesn’t create a “root” table in the dataset because there would be no rows or columns created in a table with that name.

    In the second example, try adding a second <t> element. You will still get two tables but they will be named “t” and “Stores” (instead of “root” and “Stores”). Another test is to add another different element to the second example (say <r>1</r>). In this case you will still have two tables “root” and “Stores” but now the “root” table will have 1 row with 2 columns (column “r” and column “t”).

    When the parser sees two elements with the same name it will create a table for that element and each instance of the element will be a new row in the table. When the parser sees multiple single elements under a node, it will create a table using the name of the parent node as the table name and each single element under the parent node will be a column in the table.

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

Sidebar

Related Questions

I am trying to read an XML file into a data set using dataset.ReadXml(fileName,
I'm loading data into a DataSet from an XML file using the ReadXml method.
How to read the below xml file and write its content into a plain
I was given an .xml file that I needed to read into my code
Problem description: Read an xml file, traverse to a particular node (element), if it
I am currently trying to load a slightly large xml file into a dataset.
Im trying to load a xml file into a dataset and generate crystal report
I am reading in an XML file into a Dataset, then adding a new
I have the following xml file that I need to read into my C#
I'm trying to read a xml file (from facebook) into my page. I keep

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.