Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue reading XML file into C# DataSet

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 DataSet in C# and calling dataSet.WriteXml(file, XmlWriteMode.IgnoreSchema), but this was done by someone else).

The .xml file was shaped like this:

 <?xml version="1.0" standalone="yes"?>
 <NewDataSet>
  <Foo>
    <Bar>abcd</Bar>
    <Foo>efg</Foo>
  </Foo>
  <Foo>
    <Bar>hijk</Bar>
    <Foo>lmn</Foo>
  </Foo>
</NewDataSet>

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

        DataSet ds = new DataSet();
        ds.ReadXml(file);

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

Bar     Foo-Id    Foo-Id-0
abcd     0         null
null     1         0
hijk     2         null
null     3         2

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 ds.ReadXml(file) performed in this manner, so I will be able to avoid the issue in the future. Thanks.

like image 469
Timothy Carter Avatar asked Sep 09 '08 12:09

Timothy Carter


People also ask

Why XML file is not opening?

You Don't Have the Right Program There are specific computer programs that are compatible with XML files, and you may not have one installed on your computer. The most common program is Extensible Markup Language File. Try installing this program and see if you can open the file.

How do I fix XML format?

Use the Formatting options page to specify how elements and attributes are formatted in your XML documents. To access XML formatting options, choose Tools > Options > Text Editor > XML, and then choose Formatting.


1 Answers

This appears to be correct for your nested Foo tags:

<NewDataSet>  
  <Foo>              <!-- Foo-Id: 0 -->
    <Bar>abcd</Bar>
    <Foo>efg</Foo>   <!-- Foo-Id: 1, Parent-Id: 0 -->
  </Foo>
  <Foo>              <!-- Foo-Id: 2 -->
    <Bar>hijk</Bar>
    <Foo>lmn</Foo>   <!-- Foo-Id: 3, Parent-Id: 2 -->
  </Foo>
</NewDataSet>

So this correctly becomes 4 records in your result, with a parent-child key of "Foo-Id-0"

Try:

<NewDataSet>  
  <Rec>              <!-- Rec-Id: 0 -->
    <Bar>abcd</Bar>
    <Foo>efg</Foo>   
  </Rec>
  <Rec>              <!-- Rec-Id: 1 -->
    <Bar>hijk</Bar>
    <Foo>lmn</Foo>   
  </Rec>
</NewDataSet>

Which should result in:

Bar     Foo        Rec-Id
abcd    efg        0
hijk    lmn        1
like image 175
Keith Avatar answered Oct 12 '22 17:10

Keith