Solved

exporting xml from dataset

Posted on 2006-07-18
4
379 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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