?
Solved

exporting xml from dataset

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

770 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