Dim obj_DataTable As New System.Data.DataTable("Category")
Dim obj_DataSet As New DataSet()
'Declaring the array of DataColum to hold the Primary Key Columns
Dim obj_PrimaryClmn(1) As System.Data.DataColumn
Dim obj_DataRow As System.Data.DataRow
Dim obj_DataRelation As DataRelation
Dim writer As New System.IO.StringWriter
obj_DataTable.Columns.Add(New System.Data.DataColumn("CategoryId"))
obj_DataTable.Columns.Add(New System.Data.DataColumn("CategoryName"))
obj_PrimaryClmn(0) = obj_DataTable.Columns(0)
'Assigning the CategoryId column as Primary Key
obj_DataTable.PrimaryKey = obj_PrimaryClmn
'Entering the data in Category Table
obj_DataRow = obj_DataTable.NewRow()
obj_DataRow.Item(0) = 0
obj_DataRow.Item(1) = "Select Category"
obj_DataTable.Rows.Add(obj_DataRow)
obj_DataRow = obj_DataTable.NewRow()
obj_DataRow.Item(0) = 1
obj_DataRow.Item(1) = "Computers"
obj_DataTable.Rows.Add(obj_DataRow)
obj_DataSet.Tables.Add(obj_DataTable)
obj_DataTable.PrimaryKey = obj_PrimaryClmn
Dim obj_DataRow1 As System.Data.DataRow
Dim obj_DataTable1 As New System.Data.DataTable("Product")
obj_DataTable1.Columns.Add(New System.Data.DataColumn("ProductId"))
obj_DataTable1.Columns.Add(New System.Data.DataColumn("ProductName"))
obj_DataTable1.Columns.Add(New System.Data.DataColumn("CategoryId"))
obj_DataRow1 = obj_DataTable1.NewRow()
obj_DataRow1.Item(0) = 1
obj_DataRow1.Item(1) = "PHP"
obj_DataRow1.Item(2) = 1
obj_DataTable1.Rows.Add(obj_DataRow1)
obj_DataSet.Tables.Add(obj_DataTable1)
Dim obj_ParentClmn, obj_ChildClmn As DataColumn
obj_ParentClmn = obj_DataSet.Tables("Category").Columns("CategoryId")
obj_ChildClmn = obj_DataSet.Tables("Product").Columns("CategoryId")
obj_DataRelation = New DataRelation("relation_Category_Product", obj_ParentClmn, obj_ChildClmn)
obj_DataSet.Relations.Add(obj_DataRelation)
obj_DataSet.WriteXml(writer)
MsgBox(writer.ToString())
This code executes. There is a relation between parent and child tables. This code first displays xml from first and xml from the second table one after the other. I don’t need this way. Because relation is there the xml should display first table in xml and its second table as a child node of first table. How can it be possible? Please help me out.
Set the Relation Type to
Nestedas per below code, it will populate child table as subnodes in XML.Refer here to know more about nesting of nodes, great knowledge