Treeview using xml

G

Guest

I received in this forum the following code:

private void PopulateTreeView(string xmlFile,
System.Windows.Forms.TreeView tree)
{
try
{
TreeNode t1,t2;
if (xmlFile != null && tree != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
foreach(XmlNode node in doc.SelectNodes("//course"))
{
t1 = new TreeNode(node.Attributes["title"].Value);
tree.Nodes.Add(t1);
foreach(XmlNode nod in node.SelectNodes("//module"))
{
t2 = new TreeNode(nod.Attributes["title"].Value);
t1.Nodes.Add(t2);
foreach(XmlNode n in nod.SelectNodes("//subject"))
{
t2.Nodes.Add(new TreeNode(n.FirstChild.Value));
}
}
}
}
}

This code suppose to use the xml below and build a hierarchical treeview
with the course title, under it the module title and under each module, the
lesson subjects that are under this module. I don't find the reason why, but
the code above inserts all the lesson subjects under each module, and not
only the lessons subject of the module that contain them, as I expect.

Can someone please explain why?

Thanks a lot

below is the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My
Documents\XML\csharp">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="1.4">
<subject>Organizing Controls on a Form</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="1.5">
<subject>Creating MDI Applications</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="1.1">
<exercise id="1.1.1">Creating a New Windows Form</exercise>
<exercise id="1.1.2">Inheriting a New Form from an Existing Windows
Form</exercise>
</lab>
</module>
<module id="2" title="Working with Controls" location="D:\Disk-C\Documents
and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
<lesson id="2.1">
<subject>Creating an Event Handler for a Control</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="2.2">
<subject>Using Windows Forms Controls</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="2.3">
<subject>Using Dialog Boxes in a Windows Forms Application</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="2.4">
<subject>Adding Controls at Run Time</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="2.5">
<subject>Creating Menus</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lesson id="2.6">
<subject>Validating User Input</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="2.1">
<exercise id="2.1.1">Creating and Using Controls</exercise>
</lab>
</module>
</course>
 
D

David Lloyd

Below is an updated procedure. The only change is to the line where
"//subject" appeared previously. In XPath search terminology, the "//"
terminology refers to zero or more levels in the XML document hierarchy,
which in this case means the all subject nodes in the entire document. The
"./" uses the current context (i.e. the module) to determine which nodes to
select.

private void PopulateTreeView(string xmlFile,

System.Windows.Forms.TreeView tree)

{

try

{

TreeNode t1,t2;

if (xmlFile != null && tree != null)

{

XmlDocument doc = new XmlDocument();

doc.Load(xmlFile);

foreach(XmlNode node in doc.SelectNodes("//course"))

{

t1 = new TreeNode(node.Attributes["title"].Value);

tree.Nodes.Add(t1);

foreach(XmlNode nod in node.SelectNodes("//module"))

{

t2 = new TreeNode(nod.Attributes["title"].Value);

t1.Nodes.Add(t2);

foreach(XmlNode n in nod.SelectNodes("./lesson/subject"))

{

t2.Nodes.Add(new TreeNode(n.FirstChild.Value));

}


}

}

}

}


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I received in this forum the following code:

private void PopulateTreeView(string xmlFile,
System.Windows.Forms.TreeView tree)
{
try
{
TreeNode t1,t2;
if (xmlFile != null && tree != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
foreach(XmlNode node in doc.SelectNodes("//course"))
{
t1 = new TreeNode(node.Attributes["title"].Value);
tree.Nodes.Add(t1);
foreach(XmlNode nod in node.SelectNodes("//module"))
{
t2 = new TreeNode(nod.Attributes["title"].Value);
t1.Nodes.Add(t2);
foreach(XmlNode n in nod.SelectNodes("//subject"))
{
t2.Nodes.Add(new TreeNode(n.FirstChild.Value));
}
}
}
}
}

This code suppose to use the xml below and build a hierarchical treeview
with the course title, under it the module title and under each module, the
lesson subjects that are under this module. I don't find the reason why, but
the code above inserts all the lesson subjects under each module, and not
only the lessons subject of the module that contain them, as I expect.

Can someone please explain why?

Thanks a lot

below is the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My
Documents\XML\csharp">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="1.4">
<subject>Organizing Controls on a Form</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="1.5">
<subject>Creating MDI Applications</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="1.1">
<exercise id="1.1.1">Creating a New Windows Form</exercise>
<exercise id="1.1.2">Inheriting a New Form from an Existing Windows
Form</exercise>
</lab>
</module>
<module id="2" title="Working with Controls" location="D:\Disk-C\Documents
and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
<lesson id="2.1">
<subject>Creating an Event Handler for a Control</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="2.2">
<subject>Using Windows Forms Controls</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="2.3">
<subject>Using Dialog Boxes in a Windows Forms Application</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="2.4">
<subject>Adding Controls at Run Time</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="2.5">
<subject>Creating Menus</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lesson id="2.6">
<subject>Validating User Input</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="2.1">
<exercise id="2.1.1">Creating and Using Controls</exercise>
</lab>
</module>
</course>
 
D

David Lloyd

Just an additional reference:

http://msdn.microsoft.com/library/d...html/daa75346-ccd1-4033-9250-9b9f3ba5762c.asp


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I received in this forum the following code:

private void PopulateTreeView(string xmlFile,
System.Windows.Forms.TreeView tree)
{
try
{
TreeNode t1,t2;
if (xmlFile != null && tree != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
foreach(XmlNode node in doc.SelectNodes("//course"))
{
t1 = new TreeNode(node.Attributes["title"].Value);
tree.Nodes.Add(t1);
foreach(XmlNode nod in node.SelectNodes("//module"))
{
t2 = new TreeNode(nod.Attributes["title"].Value);
t1.Nodes.Add(t2);
foreach(XmlNode n in nod.SelectNodes("//subject"))
{
t2.Nodes.Add(new TreeNode(n.FirstChild.Value));
}
}
}
}
}

This code suppose to use the xml below and build a hierarchical treeview
with the course title, under it the module title and under each module, the
lesson subjects that are under this module. I don't find the reason why, but
the code above inserts all the lesson subjects under each module, and not
only the lessons subject of the module that contain them, as I expect.

Can someone please explain why?

Thanks a lot

below is the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My
Documents\XML\csharp">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="1.4">
<subject>Organizing Controls on a Form</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="1.5">
<subject>Creating MDI Applications</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="1.1">
<exercise id="1.1.1">Creating a New Windows Form</exercise>
<exercise id="1.1.2">Inheriting a New Form from an Existing Windows
Form</exercise>
</lab>
</module>
<module id="2" title="Working with Controls" location="D:\Disk-C\Documents
and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
<lesson id="2.1">
<subject>Creating an Event Handler for a Control</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="2.2">
<subject>Using Windows Forms Controls</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="2.3">
<subject>Using Dialog Boxes in a Windows Forms Application</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="2.4">
<subject>Adding Controls at Run Time</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="2.5">
<subject>Creating Menus</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lesson id="2.6">
<subject>Validating User Input</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="2.1">
<exercise id="2.1.1">Creating and Using Controls</exercise>
</lab>
</module>
</course>
 
G

Guest

Thank you so much, you helped me a lot.

David Lloyd said:
Below is an updated procedure. The only change is to the line where
"//subject" appeared previously. In XPath search terminology, the "//"
terminology refers to zero or more levels in the XML document hierarchy,
which in this case means the all subject nodes in the entire document. The
"./" uses the current context (i.e. the module) to determine which nodes to
select.

private void PopulateTreeView(string xmlFile,

System.Windows.Forms.TreeView tree)

{

try

{

TreeNode t1,t2;

if (xmlFile != null && tree != null)

{

XmlDocument doc = new XmlDocument();

doc.Load(xmlFile);

foreach(XmlNode node in doc.SelectNodes("//course"))

{

t1 = new TreeNode(node.Attributes["title"].Value);

tree.Nodes.Add(t1);

foreach(XmlNode nod in node.SelectNodes("//module"))

{

t2 = new TreeNode(nod.Attributes["title"].Value);

t1.Nodes.Add(t2);

foreach(XmlNode n in nod.SelectNodes("./lesson/subject"))

{

t2.Nodes.Add(new TreeNode(n.FirstChild.Value));

}


}

}

}

}


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I received in this forum the following code:

private void PopulateTreeView(string xmlFile,
System.Windows.Forms.TreeView tree)
{
try
{
TreeNode t1,t2;
if (xmlFile != null && tree != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
foreach(XmlNode node in doc.SelectNodes("//course"))
{
t1 = new TreeNode(node.Attributes["title"].Value);
tree.Nodes.Add(t1);
foreach(XmlNode nod in node.SelectNodes("//module"))
{
t2 = new TreeNode(nod.Attributes["title"].Value);
t1.Nodes.Add(t2);
foreach(XmlNode n in nod.SelectNodes("//subject"))
{
t2.Nodes.Add(new TreeNode(n.FirstChild.Value));
}
}
}
}
}

This code suppose to use the xml below and build a hierarchical treeview
with the course title, under it the module title and under each module, the
lesson subjects that are under this module. I don't find the reason why, but
the code above inserts all the lesson subjects under each module, and not
only the lessons subject of the module that contain them, as I expect.

Can someone please explain why?

Thanks a lot

below is the xml file:
<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My
Documents\XML\csharp">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="1.4">
<subject>Organizing Controls on a Form</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="1.5">
<subject>Creating MDI Applications</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="1.1">
<exercise id="1.1.1">Creating a New Windows Form</exercise>
<exercise id="1.1.2">Inheriting a New Form from an Existing Windows
Form</exercise>
</lab>
</module>
<module id="2" title="Working with Controls" location="D:\Disk-C\Documents
and Settings\orit_itzhar.ATRICA\My Documents\XML\c#">
<lesson id="2.1">
<subject>Creating an Event Handler for a Control</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="2.2">
<subject>Using Windows Forms Controls</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="2.3">
<subject>Using Dialog Boxes in a Windows Forms Application</subject>
<file>winforms.pdf</file>
</lesson>
<lesson id="2.4">
<subject>Adding Controls at Run Time</subject>
<file>BnsLrnCs.zip</file>
</lesson>
<lesson id="2.5">
<subject>Creating Menus</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lesson id="2.6">
<subject>Validating User Input</subject>
<file>Working_with_Controls_Course-2555-Module-2.pdf</file>
</lesson>
<lab id="2.1">
<exercise id="2.1.1">Creating and Using Controls</exercise>
</lab>
</module>
</course>
 

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