tags:

views:

1585

answers:

2

I have a dataset that I read in from a complex xml structure....

here is a basic version of it, for the sake of the question

<cars>
    <car>
       <carName>Golf</carName>
       <engine>
          <model></model>
       </engine>
    <//car>
    <car>
       <carName>Dodge</carName>
       <engine>
          <model></model>
       </engine>
    <//car>
 </cars>

Firstly I create the dataset to get the whole document.... (and I read this from file)

 DataSet dsCars = new DataSet();
 dsConfiguration.ReadXml("allcars.xml"));

Next I want to loop through all car rows.. to do this I use the following code:

 foreach(DataRow carDataRow in dsCars.Tables["Car"].Rows)

Now in this loop, I want to inject the car row (AND all its sub content) into a new dataset

So in the foreach loop I have code to

  1. Create the new dataset
  2. Create a new datatable = CarList2
  3. Import the row using : tempTaskTable2.ImportRow(carDataRow);

But when I serialize to file using: tempTaskDS.WriteXml(@"c:\test.xml");

The newly created datatable = Carlist2 is blank and contains no data at all...

What am I doing wrong?

+1  A: 

I'm not sure what you are trying to do but you could try this in the loop:

DataSet tempTaskDS = new DataSet("tempCars");
tempTaskDS.Tables.Add("Cars");
tempTaskDS.Tables[0].Rows.Add(carDataRow);
Jonas Elfström
Not the table. Just the row
JL
A: 

First make sure your target DataSet / DataTable is the same as the source DataSet / DataTable with regards to column count, column types etc. then use

targetDataSet.Tables["CarList2"].ImportRow(sourceRow)
Colin