Solved

exporting xml from dataset

Posted on 2006-07-18
4
377 Views
Last Modified: 2008-02-20
Is this xml format possible to create from a dataset writeXML method? I haven't been able to create multiple tables to look like this:

 <document>
  <employee>
    <name>
      <lastname>Kelly</lastname>
      <firstname>Grace</firstname>
    </name>
    <hiredate>October 15, 2005</hiredate>
    <projects>
      <project>
        <product>Printer</product>
        <id>111</id>
        <price>$111.00</price>
      </project>
      <project>
        <product>Laptop</product>
        <id>222</id>
        <price>$989.00</price>
      </project>
    </projects>
  </employee>
</document>
0
Comment
Question by:lytung818
  • 2
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
sagacitysolutions earned 500 total points
ID: 17134703
This is not a complete solution but its a start and I hope it helps


I did this by reading an xml document into a dataset and rebuilding the dataset from scratch
So save your XML doxument then declare a dataset and do the following

Dataset.ReadXML("FULLPATHNAME")

Then discover the tables and the datarelations
This code will get you somewhat closer but doesnt exactly output as you would like

        Dim myDs As New DataSet("Document")
        Dim myDs2 As New DataSet("Document")

        Dim myTb As New DataTable("employee")
        Dim mytbr1 As DataRelation
        Dim mytbr2 As DataRelation

        Dim myTb2 As New DataTable("name")
        Dim myTb3 As New DataTable("projects")
        Dim mytb3r1 As DataRelation


        Dim myTb4 As New DataTable("project")

        myTb.Columns.Add(AddDatacolumn("employee_id", GetType(System.Int32), MappingType.Hidden, True, 0, 1, "employee_id", False, True))
        myTb.Columns.Add(AddDatacolumn("hiredate", GetType(System.String), MappingType.Element, False, 0, 1, "hiredate", True, False))

        myTb2.Columns.Add(AddDatacolumn("lastname", GetType(System.String), MappingType.Element, False, 0, 1, "lastname", True, False))
        myTb2.Columns.Add(AddDatacolumn("firstname", GetType(System.String), MappingType.Element, False, 0, 1, "firstname", True, False))
        myTb2.Columns.Add(AddDatacolumn("employee_id", GetType(System.Int32), MappingType.Hidden, True, 0, 1, "employee_id", False, True))

        myTb3.Columns.Add(AddDatacolumn("projects_id", GetType(System.Int32), MappingType.Hidden, True, 0, 1, "projects_id", False, True))
        myTb3.Columns.Add(AddDatacolumn("employee_id", GetType(System.Int32), MappingType.Hidden, False, 0, 1, "employee_id", True, False))

        myTb4.Columns.Add(AddDatacolumn("product", GetType(System.String), MappingType.Element, False, 0, 1, "product", True, False))
        myTb4.Columns.Add(AddDatacolumn("id", GetType(System.String), MappingType.Element, False, 0, 1, "id", True, False))
        myTb4.Columns.Add(AddDatacolumn("price", GetType(System.String), MappingType.Element, False, 0, 1, "price", True, False))
        myTb4.Columns.Add(AddDatacolumn("projects_id", GetType(System.Int32), MappingType.Hidden, False, 0, 1, "projects_id", True, False))


        Dim myDr As DataRow
        Dim myDr2 As DataRow
        Dim mydr3 As DataRow
        Dim mydr4 As DataRow

        myDr = myTb.NewRow
        myDr("employee_id") = 0
        myDr("HireDate") = "October 15, 2005"
        myTb.Rows.Add(myDr)

        myDr2 = myTb2.NewRow
        myDr2("employee_id") = 0
        myDr2("FirstName") = "Grace"
        myDr2("LastName") = "Kelly"
        myTb2.Rows.Add(myDr2)

        mydr3 = myTb3.NewRow
        mydr3("projects_id") = 0
        mydr3("employee_id") = 0
        myTb3.Rows.Add(mydr3)

        mydr4 = myTb4.NewRow
        mydr4("product") = "Printer"
        mydr4("id") = "111"
        mydr4("price") = "$111.00"
        myTb4.Rows.Add(mydr4)

        mydr4 = myTb4.NewRow
        mydr4("product") = "Laptop"
        mydr4("id") = "222"
        mydr4("price") = "$989.00"
        myTb4.Rows.Add(mydr4)

        myDs.Tables.Add(myTb)
        myDs.Tables.Add(myTb2)
        myDs.Tables.Add(myTb3)
        myDs.Tables.Add(myTb4)

        mytbr1 = New DataRelation("employee_name", myTb.Columns("employee_id"), myTb2.Columns("employee_id"), True)
        mytbr2 = New DataRelation("employee_projects", myTb.Columns("employee_id"), myTb3.Columns("employee_id"), True)
        mytb3r1 = New DataRelation("projects_project", myTb3.Columns("projects_id"), myTb4.Columns("projects_id"), True)



        myDs.Relations.Add(mytb3r1)
        myDs.Relations.Add(mytbr1)
        myDs.Relations.Add(mytbr2)

        myDs.EnforceConstraints = True
        myDs.WriteXml("C:\test2.txt")


    Private Function AddDatacolumn(ByVal ColumnName As String, ByVal Datatype As Type, ByVal MappingType As System.Data.MappingType, ByVal AutoIncrement As Boolean, ByVal AutoIncrementSeed As Int32, ByVal AutoIncrementStep As Int32, ByVal Caption As String, ByVal AllowDBNull As Boolean, ByVal Unique As Boolean) As DataColumn
        Dim myc As New DataColumn(ColumnName, Datatype, "", MappingType)
        myc.AutoIncrement = AutoIncrement
        myc.AutoIncrementSeed = AutoIncrementSeed
        myc.AutoIncrementStep = AutoIncrementStep
        myc.Caption = Caption
        myc.AllowDBNull = AllowDBNull
        myc.Unique = Unique
        Return myc

    End Function
0
 

Author Comment

by:lytung818
ID: 17134911
Thanks so much for the reply. I have been sorta working on the first part similar to what you have..however i am having trouble adding the rows. I dont want to hardcode the rows since i dont know how many there could be. I tried something like this, and it didn't work :

  Dim i As Integer = intCounter
            Dim rowName As DataRow

            rowName = Table2.NewRow()
            rowName.Item(intCounter) = datafield
            Table2.Rows.Add(rowName)

            intCounter = intCounter + 1

also, what do the relations do?
0
 
LVL 8

Expert Comment

by:sagacitysolutions
ID: 17135018
It defines the relationship between the tables within the dataset

What error are you getting?
0
 

Author Comment

by:lytung818
ID: 17135504
NO error. I just needed an example on how this would work. I tried it and it woked fine, however i dont want to hardcode the rows.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now