TreeView datasource ?

G

Guest

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
 
J

jabailo

serge said:
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;

if(xNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("row").InnerText.ToString() + " " +
xNode.Attributes.GetNamedItem("bay").InnerText.ToString() + " " +
xNode.Attributes.GetNamedItem("level").InnerText.ToString()
));


if(xNode.Name=="client")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("id").InnerText.ToString()));

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




tNode = inTreeNode.Nodes;
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").InnerText.ToString()
));

}

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

Guest

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 ?

serge said:
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;

if(xNode.Name=="position")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("row").InnerText.ToString() + " " +
xNode.Attributes.GetNamedItem("bay").InnerText.ToString() + " " +
xNode.Attributes.GetNamedItem("level").InnerText.ToString()
));


if(xNode.Name=="client")
inTreeNode.Nodes.Add(new TreeNode(
xNode.Attributes.GetNamedItem("id").InnerText.ToString()));

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




tNode = inTreeNode.Nodes;
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").InnerText.ToString()
));

}

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top