473,382 Members | 1,431 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,382 software developers and data experts.

TreeView datasource ?

dear all,

Is there an easy way to bind a treeview control with an XML object as
datasource?
In a similar way as the dataset is doing, and build columns accroding to
XML node, I could imagine a treeview which build its notes accordind to XML
object too

how can I do that?

thanks for wour help
serge
Jul 21 '05 #1
2 8013
serge calderara wrote:
dear all,

Is there an easy way to bind a treeview control with an XML object as
datasource?
In a similar way as the dataset is doing, and build columns accroding to
XML node, I could imagine a treeview which build its notes accordind to XML
object too

how can I do that?

thanks for wour help
serge


Funny you should ask, becase I am working on a project where i do just
that. I am retrieving XML data from a web service and I use that to
populate a tree control. My XML data is highly tailored to the
application, which is why you see special parsing in the AddNode method.

private void populateBox()
{
//Object[] objarray = lc.AllLocations();

lcx = lc.LocationsTopLevel();

try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();

//dom.AppendChild(lcx);

//dom.Load(textBox1.Text);
dom.AppendChild(dom.ImportNode(
lcx
,true));

//inXmlNode=inXmlNode.SelectSingleNode("//position[@row='1']");

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode);
treeView1.ExpandAll();

}
catch(XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}


private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;

int i;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.

try
{

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for(i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];

if(xNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("row").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("bay").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("level").InnerText.T oString()
));
if(xNode.Name=="client")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("id").InnerText.ToSt ring()));

if(xNode.Name=="partnum")
inTreeNode.Nodes.Add(new TreeNode(
xNode.InnerText));


tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
//inTreeNode.Text = (inXmlNode.OuterXml).Trim();
//inTreeNode.Text = (inXmlNode.InnerText).Trim();
if(inXmlNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
inXmlNode.Attributes.GetNamedItem("row").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("bay").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("level").InnerTe xt.ToString()
));

}

}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}
Jul 21 '05 #2
I have a crystal ball...
Its is strange that XML is a hierarchical structure of any type of data an
nothing is existing on just placeing an XML source as a datasource for a
treeview, as treeview is really matching that XMl node.

Have you seen or heard something in 2005 beta 2 on that topic ?

"ja*****@texeme.com" wrote:
serge calderara wrote:
dear all,

Is there an easy way to bind a treeview control with an XML object as
datasource?
In a similar way as the dataset is doing, and build columns accroding to
XML node, I could imagine a treeview which build its notes accordind to XML
object too

how can I do that?

thanks for wour help
serge


Funny you should ask, becase I am working on a project where i do just
that. I am retrieving XML data from a web service and I use that to
populate a tree control. My XML data is highly tailored to the
application, which is why you see special parsing in the AddNode method.

private void populateBox()
{
//Object[] objarray = lc.AllLocations();

lcx = lc.LocationsTopLevel();

try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();

//dom.AppendChild(lcx);

//dom.Load(textBox1.Text);
dom.AppendChild(dom.ImportNode(
lcx
,true));

//inXmlNode=inXmlNode.SelectSingleNode("//position[@row='1']");

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode);
treeView1.ExpandAll();

}
catch(XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}


private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList nodeList;

int i;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.

try
{

if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for(i = 0; i<=nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];

if(xNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("row").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("bay").InnerText.ToS tring() + " " +
xNode.Attributes.GetNamedItem("level").InnerText.T oString()
));
if(xNode.Name=="client")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("id").InnerText.ToSt ring()));

if(xNode.Name=="partnum")
inTreeNode.Nodes.Add(new TreeNode(
xNode.InnerText));


tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
// Here you need to pull the data from the XmlNode based on the
// type of node, whether attribute values are required, and so forth.
//inTreeNode.Text = (inXmlNode.OuterXml).Trim();
//inTreeNode.Text = (inXmlNode.InnerText).Trim();
if(inXmlNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
inXmlNode.Attributes.GetNamedItem("row").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("bay").InnerText .ToString() + " " +
inXmlNode.Attributes.GetNamedItem("level").InnerTe xt.ToString()
));

}

}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
}

Jul 21 '05 #3

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

Similar topics

2
by: Craig | last post by:
Is there a way to "tie" or "bind" an XmlDocument to a treeView, so the changes in the treeView can automatically be reflected in the XmlDocument object? (Like DataSource for a ListBox) If not,...
2
by: Ryo | last post by:
Hello ! I have an XmlDocument in memory and I want to put this document as datasource of my treeview. How to do this ? Thanks.
0
by: Ryo | last post by:
Hello ! I have this simple aspx page: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_treeview.aspx.cs" Inherits="test_treeview" Title="Treeview" %> <asp:Content...
2
by: serge calderara | last post by:
dear all, Is there an easy way to bind a treeview control with an XML object as datasource? In a similar way as the dataset is doing, and build columns accroding to XML node, I could imagine a...
0
by: Michael Lewis | last post by:
Does anyone know if it is possible to bind a gridview to a treeview. Ie. I want to be able to expand a treeview node and see a separate gridview for each node. The gridview will be filtered by a...
5
by: Paul | last post by:
Hi, I am a self taught VBA programmer, and I'm trying to learn VB2005 Express (The price was right). I like the look of the treeview control, and I'd like to use it as a menu system for my users,...
26
by: JJ | last post by:
Is there any way you can expand a parent node on the treeview control _without a postback_ by clicking on the node text (NOT clicking the expand image URL) ? I want to format the treeview node...
3
by: =?Utf-8?B?TGVzbGll?= | last post by:
Using Visual Studio 2005 SP1 I am attempting to dynamically load a treeview control. I create an XmlDataSource and then load the data source using XmlDataSource.Data. I Load my XML string into...
0
by: Dennis Francek | last post by:
Hello I have populated a treeview from my dataset and by clicking in the treeview i want to get the corresponding table shown in a datagridview. Ive managed this by looking at each DataRow and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.