[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

exporting xml from dataset

Posted on 2006-07-18
4
Medium Priority
?
388 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month20 days, 6 hours left to enroll

872 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