Hi,
I tried the ado.Net writeXml one more time. :-)
I managed to somehow serialize the entities to a xml file.
The solution I found to pick up wich entities will be written to the file is to:
* create a dataset and clone the myEntity.dataset to get the structure
* make a list of datarows that I will fill by iterating the child collections of the entities
* merge the rows in the dataset
* call the writeXml method
The result would require a fair amount of Xquerying because the elements are all under the rotelement "newDataset" (no structure)
It wasn't so much work to write my own serializer, and the resulting file is much easier to parse, so I will stick with it. It also make it easier to not expose columns that should remain private.
That was the code I wrote, if of any interest... I wouldn't be surprised if it is a very wrong way to do it :). (example with 2 levels of nexting).
Dim path As String = <thePath>
Dim wrSettings As New XmlWriterSettings()
With wrSettings
.Indent = True
.Encoding = Text.Encoding.UTF8
.CloseOutput = True
.ConformanceLevel = ConformanceLevel.Auto ' must have that
End With
Dim writer As XmlWriter = XmlWriter.Create(path, wrSettings)
Using writer
Try
Dim ds As DataSet = context.Table.DataSet.Clone
Dim rowList As New List(Of DataRow)
rowList.Add(context)
For Each child As MyChild In context.Childs
rowList.Add(child)
For Each grandchild As grandchild In MyChild.Childs
rowList.Add(grandchild)
Next
Next
Dim theRows As DataRow() = rowList.ToArray()
ds.Merge(theRows)
ds.WriteXml(writer, XmlWriteMode.IgnoreSchema)
writer.Close()
Catch ex As Exception
Diagnostics.Debug.Print("Exception from xmlWriter: " & ex.Message) 'to get a good message
Throw ex ' caller's work
End Try
End Using
|