DataSet.ReadXML has strange results

C

CSharp-Jay

Demo XML:

<?xml version="1.0" encoding="utf-8" ?>
<BuildingLayouts>
<EcoLayouts>
<Layout>
<Name>Test EcoLayout</Name>
<Image>.\images\TestEcoLayout.png</Image>
</Layout>
</EcoLayouts>
<TycoonLayouts>
<Layout>
<Name>Test TycoonLayout</Name>
<Image>.\images\TestTycoonLayout.png</Image>
</Layout>
</TycoonLayouts>
<TechLayouts>
<Layout>
<Name>Test TechLayout</Name>
<Image>.\images\TestTechLayout.png</Image>
</Layout>
</TechLayouts>
</BuildingLayouts>

Code:

DataSet xDS = new DataSet();
xDS.ReadXml(@".\data\Layouts.xml");
for (int i = 0; i < xDS.Tables.Count; i++)
{
Console.WriteLine(xDS.Tables.TableName.ToString());
}

Console Results:

EcoLayouts
Layout <--- what?
TycoonLayouts
TechLayouts

I have tried this in numerous ways and flavors, completely changing
the format of the XML every time, yet it keeps counting one of the
records as a table?
 
M

Marcel Müller

Demo XML:

<?xml version="1.0" encoding="utf-8" ?>
<BuildingLayouts>
<EcoLayouts>
<Layout>
<Name>Test EcoLayout</Name>
<Image>.\images\TestEcoLayout.png</Image>
</Layout>
</EcoLayouts>
<TycoonLayouts>
<Layout>
<Name>Test TycoonLayout</Name>
<Image>.\images\TestTycoonLayout.png</Image>
</Layout>
</TycoonLayouts>
<TechLayouts>
<Layout>
<Name>Test TechLayout</Name>
<Image>.\images\TestTechLayout.png</Image>
</Layout>
</TechLayouts>
</BuildingLayouts>

Code:

DataSet xDS = new DataSet();
xDS.ReadXml(@".\data\Layouts.xml");
for (int i = 0; i< xDS.Tables.Count; i++)
{
Console.WriteLine(xDS.Tables.TableName.ToString());
}

Console Results:

EcoLayouts
Layout<--- what?
TycoonLayouts
TechLayouts

I have tried this in numerous ways and flavors, completely changing
the format of the XML every time, yet it keeps counting one of the
records as a table?


AFAIK if you do not specify any structure information, a table is
created for each XML node type that is not a leaf. But nodes with the
same name in different contexts are unsupported. So it does not create 3
Layout tables but only one.

Create a strongly typed DataSet class with an XSD of your XML structure
and then use ReadXml with that type.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top