views:

89

answers:

1

I've go the following XML file...

<A>
  <B>
    <C>ValueX</C>
    <D>ValueY</D>
  </B>
</A>

Which I read into a DataSet to display it in a DataGridView.

DataSet ds = new DataSet();
DataTable t = new DataTable("B");
ds.Tables.Add(t);
t.Columns.Add("C", typeof(string));
t.Columns.Add("D", typeof(string));
// bind to DataGridView
ds.ReadXml(file);

But when I write it back using the follwing command...

ds.WriteXml(file);

the nested structure of the file is destroyed.

<A>
  <C>ValueX</C>
  <D>ValueY</D>
</A>

Any ideas how to preserve the stcuture.

A: 

Setting a xsd file and readxml should be sufficient. I think there is no need for the tables and columns.

Michel van Engelen
I tried generating the DataSet from xsd using xsd.exe. That lead to other problems. So I'd like to do it by hand as a learning step.
Stefan Teitge
Hi Stefan, you don't have to use xsd.exe. Create a xsd by hand, and read it with dataset.readxmlschema.
Michel van Engelen