Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to properly convert a datatable into an XML file

Status
Not open for further replies.

Belcebu

Programmer
Aug 3, 2005
9
US
Hello,

I am trying to convert an existing datatable into an XML file. The datatable has many fields including the two most important ones:

parent_guid
current_guid

I want to create the XML out of this datatable with the relationship of these two columns. A simple WriteXml method does not solve my problem because I it does not relate these two columns.

I am trying to create a dataset and add relations to it, however, I have been unable to make it work since it is looking for two tables to create the relationship. Thus I tried just copying the same table and providing that but it does not work. This seems like a simple thing to do, and so I was looking for maybe a quick example of how to accomplish this.

I was actually trying something along these lines:

Public Shared Sub convertDTtoXML(ByVal dt As Data.DataTable)

Dim ds As New Data.DataSet
Dim dt1 As Data.DataTable = New Data.DataTable


dt1 = dt.Copy()
ds.Tables.Add(dt)
ds.Tables.Add(dt1)


ds.Relations.Add("parent_guid", _
New Data.DataColumn() {dt.Columns(1)}, _
New Data.DataColumn() {dt1.Columns(2)}, True)


dt.WriteXml(location)


Any help is appreciated!!!

Thank You!!
 
Your heirarchy seems backwards to me, because you have to start at the bottom, instead of the top, which is opposite of XML.


The following is psuedo code:

Assuming your structure you will need a recursive loop that acts sorta like this:

Public Function XMLChunk(curGuid as Guid, dt as Datatable) as XMLNode
Dim rtnNode as XMLNode
For each oRow as DataRow in dt.Rows
if oRow.Parent_Guid = curGuid then
'we have a child node
Dim oNode as XMLNode = XMLChunk(oRow.Current_GUID)
if not oNode is nothing
rtnNode.AddNode(oNode)
end if
end if
Next
if rtnNode.Nodes.Count > 0 then
'add current node data
rtnNode.Text = "some values here"
end if
return rtnNode
end Function





-Sometimes the answer to your question is the hack that works
 
Qik3Coder thanks for the help. I was hoping that instead of having to create the xml file node by node that there was a way to leverage the writexml method by somehow relating the perent_guids to the guids.

If that is not possible then I will have to make a recursive method like yours (which I'm already kind of working on).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top