Print Page | Close Window

writing an entity with child to xml

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=432
Printed Date: 12-May-2026 at 9:26pm


Topic: writing an entity with child to xml
Posted By: Dominique
Subject: writing an entity with child to xml
Date Posted: 18-Sep-2007 at 12:22am
Hi,
I want to write a parent-childs hierarchy as an xml-file. I am wondering if there is a trivial way to achieve this with Ideablade?
I actually don't know the dotnet xml-API. I saw yesterday that datatable had method to do that but I didn't achieve my goal wich is basically

sub WriteEntitty(anEntity as MyType)
anEntity = persistMng.getEntity(..)
anEntity.MagicMethodToXML(path, encoding)

the following structure for the xml file would be perfect:

<parent>
<tblColumnname>value<\tblColumnname>
<tblColumnname>value<\tblColumnname>
<evt- childtype1s>
    <child type1>
      <tblColumnname>value<\tblColumnname>
    </child type1>
    <child type1>
      <tblColumnname>value<\tblColumnname>
    </child type1>
</evt- childtype1s>
<evt- childtype2s>
    <child type2>
      <tblColumnname>value<\tblColumnname>
      <tblColumnname>value<\tblColumnname>
    </child type2>
    <child type2>
      <tblColumnname>value<\tblColumnname>
      <tblColumnname>value<\tblColumnname>
    </child type2>
</evt- childtype2s>
</parent>


Dominique

-------------
Dominique



Replies:
Posted By: davidklitzke
Date Posted: 18-Sep-2007 at 8:55am

I would think that you would be able to use the DataSet.WriteXml method.



Posted By: Dominique
Date Posted: 18-Sep-2007 at 12:18pm
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


-------------
Dominique



Print Page | Close Window