mardi 5 mai 2015

XMLReader loses embedded depth when elements have same name

I have an Xml file that contains multiple levels of layers, for example:

<?xml version="1.0" encoding="utf-8"?>
<TestSet>
  <Test>
    <Test>
      <Step/>
    <Test>
    <Test>
      <Step/>
    </Test>
  </Test>
</TestSet>

As you can see there are two tests embedded within another test. When I read this into a DataSet I expect 4 tables, one table for each level of embedding.

DataSet.Table[0].Rows.Count == 1   // TestSet
DataSet.Table[1].Rows.Count == 1   // High level Test
DataSet.Table[2].Rows.Count == 2   // Low level Test
DataSet.Table[3].Rows.Count == 2   // Step

I am parsing the file using XmlReader like so:

XmlReader xmlFile;
xmlFile = XmlReader.Create(file, new XmlReaderSettings());
DataSet data = new DataSet();
data.ReadXml(xmlFile);

Instead when I use XmlReader I get:

DataSet.Table[0].Rows.Count == 3   // All of the Test items, High and Low level
DataSet.Table[1].Rows.Count == 2   // Step

Two problems, why does it skip over my TestSet level, and why does it combine my Test items into one table, despite being different levels.

Is there another reader I should use that would maintain the integrity of my data, or are there specific settings for XmlReader that would solve these issues, or am I better off reading in and parsing the file myself?

Aucun commentaire:

Enregistrer un commentaire