New Posts New Posts RSS Feed: writing an entity with child to xml
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

writing an entity with child to xml

 Post Reply Post Reply
Author
Dominique View Drop Down
Groupie
Groupie
Avatar

Joined: 28-Jun-2007
Location: Norway
Posts: 44
Post Options Post Options   Quote Dominique Quote  Post ReplyReply Direct Link To This Post Topic: writing an entity with child to xml
    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
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post Posted: 18-Sep-2007 at 8:55am

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

Back to Top
Dominique View Drop Down
Groupie
Groupie
Avatar

Joined: 28-Jun-2007
Location: Norway
Posts: 44
Post Options Post Options   Quote Dominique Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down