473,405 Members | 2,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

convert sql tables with parent-child keys of a nested structure into a nested xml file

I have 5 tables in SQL Server. Each with the following design and a sample
chain of the relationships from the root (WRL - World)
UUS is the 'Code' of the first table and it is the 'Parent' value of the
second table, etc.

Parent varchar 3
Name varchar 60
Code varchar 3

WRL United States UUS <- UUS California UCA <- UCA North Coast UNC <- UNC
Sonoma County USO <- USO Russian River Valley URR

If I have 6 tables with these relationships from root to these vineyard
appellation locations, how could I convert this nested, hiearchical data
from SQL Server tables into a nested XML file? I will be using the XML
file as input for a Winforms Treeview display of this data.
Thank you! -greg

ps. I just saw this newsgroup after posting to SQL Server XML newsgroup.
Nov 12 '05 #1
1 3051
From what I have read since I posted this about FOR XML EXPLICIT, I think it
is time for me to step into the comfort of SQL Server 2005 and give it a
test ride. Until then what I did was this. I built the Treeview after
creating one table as per
http://www.wwwcoder.com/main/parenti...8/default.aspx
then converted that recursively produced treeview node structure into the
necessary nested xml per Derek Harmon's excellent C# methods which I
massaged into vb.net for this app.
-Greg Hazzard

Here is Derek's code since I can't send a link to it;

using System;
using System.IO;
using System.Windows.Forms;
using System.Xml;

// . . .

private void SaveNode( XmlElement eNodes, TreeNodeCollection tnc)
{
foreach( TreeNode n in tnc)
this.SaveNode( eNodes, n);
}

private void SaveNode( XmlElement eNodes, TreeNode n )
{
XmlDocument nodeFactory = eNodes.OwnerDocument;
XmlElement child = nodeFactory.CreateElement( "Node");
XmlAttribute text = nodeFactory.CreateAttribute( "Text");
text.Value = n.Text;
child.Attributes.Append( text);
if ( null != n.Nodes && n.Nodes.Count > 0 )
{
XmlElement grandchildren = nodeFactory.CreateElement( "Nodes");
child.AppendChild( grandchildren);
this.SaveNode( grandchildren, n.Nodes);
}
eNodes.AppendChild( child);
}

private void btnSave_Click(object sender, System.EventArgs e)
{
XmlDocument doc = new XmlDocument( );
XmlElement eDoc = doc.CreateElement( "TreeView");
if ( null != this.treeView1.Nodes && this.treeView1.Nodes.Count > 0 )
{
XmlElement eNodes = doc.CreateElement( "Nodes");
this.SaveNode( eNodes, this.treeView1.Nodes);
eDoc.AppendChild( eNodes);
}
doc.AppendChild( eDoc);
XmlTextWriter sink = new XmlTextWriter( "../../tree.xml",
System.Text.Encoding.UTF8);
try
{
doc.WriteTo( sink);
sink.Flush( );
}
finally
{
sink.Close( );
}
}
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
2
by: Patrick Finnegan | last post by:
DB2DART detects problems with SYSIBM.SYSSECTION and SYSIBM.SYSSTMT .. I cannot connect to or drop these tables(system objects). What are my options besides a db restore? Thanks in advance. ...
3
by: Seba | last post by:
For db2 UDB I need to get dependency information for views. What I'm looking for is the tables that have the source information for the view. i.e. : if the following view was defined in my...
25
by: prabhat143 | last post by:
Hi, Given a singly linked, null terminated list, how can it be converted to tree? Each node in the list has three attributes: it's ID, it's parent ID and of course, the next node it's pointing...
9
by: ReidarT | last post by:
I have a value from a field (int) in a table like string strPlayerLoginStatus = dsPlayer2.Tables.Rows.ToString(); I like to convert the strPlayerLoginStatus from string to int to use it in an if...
29
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL...
15
by: angellian | last post by:
Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6...
22
by: amygdala | last post by:
Hi, I'm trying to grasp OOP to build an interface using class objects that lets me access database tables easily. You have probably seen this before, or maybe even built it yourself at some...
1
by: Landon | last post by:
Ver: Access 2003 Hi everyone and thanks in advance, General Issue: I want to make a table whose data polls from two other tables and will update (add new records) if additions are made to...
1
by: DMAGIC448 | last post by:
I am trying to figure out how to add records to multiple tables w/ one form. I am making a database for a Home DayCare and I want to add/update records for 5 tables (Student info, Parent 1 Info,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.