<p>I was given an .xml file that I needed to read into my code as a DataSet (as background, the file was created by creating a <code>DataSet</code> in C# and calling <code>dataSet.WriteXml(file, XmlWriteMode.IgnoreSchema)</code>, but this was done by someone else). </p>

<p>The .xml file was shaped like this:</p>

<pre><code> &lt;?xml version="1.0" standalone="yes"?&gt;
 &lt;NewDataSet&gt;
  &lt;Foo&gt;
    &lt;Bar&gt;abcd&lt;/Bar&gt;
    &lt;Foo&gt;efg&lt;/Foo&gt;
  &lt;/Foo&gt;
  &lt;Foo&gt;
    &lt;Bar&gt;hijk&lt;/Bar&gt;
    &lt;Foo&gt;lmn&lt;/Foo&gt;
  &lt;/Foo&gt;
&lt;/NewDataSet&gt;
</code></pre>

<p>Using C# and .NET 2.0, I read the file in using the code below:</p>

<pre><code>        DataSet ds = new DataSet();
        ds.ReadXml(file);
</code></pre>

<p>Using a breakpoint, after this <code>line ds.Tables[0]</code> looked like this (using dashes in place of underscores that I couldn't get to format properly):</p>

<pre><code>Bar     Foo-Id    Foo-Id-0
abcd     0         null
null     1         0
hijk     2         null
null     3         2
</code></pre>

<p>I have found a workaround (I know there are many) and have been able to successfully read in the .xml, but what I would like to understand why <code>ds.ReadXml(file)</code> performed in this manner, so I will be able to avoid the issue in the future.  Thanks.</p>

[{'answer_id': 51777, 'author': 'rohancragg', 'author_id': 5351, 'author_profile': 'https://Stackoverflow.com/users/5351', 'pm_score': 0, 'selected': False, 'text': "<p>These are my observations rather than a full answer:</p>\n\n<p>My guess (without trying to re-produce it myself) is that a couple of things may be happening as the DataSet tries to 'flatten' a hierarchical structure to a relational data structure.</p>\n\n<p>1) thinking about the data from a relational database perspective; there is no obvious primary key field for identifying each of the Foo elements in the collection so the DataSet has automatically used the ordinal position in the file as an auto-generated field called Foo-Id.</p>\n\n<p>2) There are actually two elements called 'Foo' so that probably explains the generation of a strange name for the column 'Foo-Id-0' (it has auto-generated a unique name for the column - I guess you could think of this as a fault-tolerant behaviour in the DataSet).</p>\n"}, {'answer_id': 51867, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 3, 'selected': True, 'text': '<p>This appears to be correct for your <em>nested</em> Foo tags:</p>\n\n<pre><code>&lt;NewDataSet&gt;  \n  &lt;Foo&gt;              &lt;!-- Foo-Id: 0 --&gt;\n    &lt;Bar&gt;abcd&lt;/Bar&gt;\n    &lt;Foo&gt;efg&lt;/Foo&gt;   &lt;!-- Foo-Id: 1, Parent-Id: 0 --&gt;\n  &lt;/Foo&gt;\n  &lt;Foo&gt;              &lt;!-- Foo-Id: 2 --&gt;\n    &lt;Bar&gt;hijk&lt;/Bar&gt;\n    &lt;Foo&gt;lmn&lt;/Foo&gt;   &lt;!-- Foo-Id: 3, Parent-Id: 2 --&gt;\n  &lt;/Foo&gt;\n&lt;/NewDataSet&gt;\n</code></pre>\n\n<p>So this correctly becomes 4 records in your result, with a parent-child key of "Foo-Id-0"</p>\n\n<p>Try:</p>\n\n<pre><code>&lt;NewDataSet&gt;  \n  &lt;Rec&gt;              &lt;!-- Rec-Id: 0 --&gt;\n    &lt;Bar&gt;abcd&lt;/Bar&gt;\n    &lt;Foo&gt;efg&lt;/Foo&gt;   \n  &lt;/Rec&gt;\n  &lt;Rec&gt;              &lt;!-- Rec-Id: 1 --&gt;\n    &lt;Bar&gt;hijk&lt;/Bar&gt;\n    &lt;Foo&gt;lmn&lt;/Foo&gt;   \n  &lt;/Rec&gt;\n&lt;/NewDataSet&gt;\n</code></pre>\n\n<p>Which should result in:</p>\n\n<pre><code>Bar     Foo        Rec-Id\nabcd    efg        0\nhijk    lmn        1\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51741', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4660/']