Solved

exporting xml from dataset

Posted on 2006-07-18
4
382 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

728 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