How To populate a Treeview with an xml-file ???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I guess the subject is quite explicit ;
I have a couple of xml files which I would like to use to poulate a TreeView. Is this feasible ??
I'd also like to (if that is possible) to load only certain nodes of the tree and not its entirity.
Something like :

Parent
Child
Child
Child
Parent
Parent

So it would only load Parent 1 to 3 and Child1 to 3 and not the Childs of Parents 1 and 2. Those only have to be loaded when the user wants top extend those nodes..
Any help would really be appreciated, cause i'm near desperation
 
This will get you started.

http://support.microsoft.com/default.aspx?scid=kb;en-us;Q317597

There is a link on the page to a VB.Net version if that is your language of
choice.

HTH

Geoff
tiger79 said:
I guess the subject is quite explicit ;)
I have a couple of xml files which I would like to use to poulate a TreeView. Is this feasible ???
I'd also like to (if that is possible) to load only certain nodes of the tree and not its entirity.
Something like :

Parent1
Child1
Child2
Child3
Parent2
Parent3

So it would only load Parent 1 to 3 and Child1 to 3 and not the Childs of
Parents 1 and 2. Those only have to be loaded when the user wants top extend
those nodes...
 
thanx, only this is a version for .Net and not .Net CF :

// 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()

now it gives me an error here : AddNode(dom.DocumentElement, tNode)
error : The name 'AddNode' does not exist in the class or namespace 'XMLFromFile.Form1

so, is there some other function, class or whatever it is which does the same in CF ??
 
Ok, I found out ;
I still had to incorporate the called function... stupid of me...

anyways, i get an error here : this.textBox1.Text = Application.StartupPath + "\\Sample.xml"
'System.Windows.Forms.Application' does not contain a definition for 'StartupPath
Is there some CF comand which tells the application where to get the xml file ??
thanx
 
I have included the xml file in th project so now it wont give me the error i mentioned above..
but it still wont work :
I get an error-window saying : FileNotFoundException..
I do get what the problem is (he cant find he file) but how can I solve this ??
Do I have to include the xml file in another way (I include it through Project--> Add existing Item) ??
Or do I have to change te location of the xml file ???
 
Are you trying to find the path of your application. If so:-

TextBox1.Text =
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssem
bly().GetName().CodeBase)

HTH,

Geoff
tiger79 said:
I have included the xml file in th project so now it wont give me the error i mentioned above...
but it still wont work :(
I get an error-window saying : FileNotFoundException...
I do get what the problem is (he cant find he file) but how can I solve this ???
Do I have to include the xml file in another way (I include it through
Project--> Add existing Item) ???
 
now i got it working, I had to copy manually the xml file in the Windows directory of my pocketpc, after which I told him to loon into that directory : dom.Load("\\windows\\ATT_214_PREDEFINED_VALUES.XML")

but now i get another error

XmlNodeList nodelist = dom.SelectNodes("//child")

'System.Xml.XmlDocument' does not contain a definition for 'SelectNodes

and that sucks :
How do I only read certain tags from the file ??? Say i'd like to only place the cdata in my tree ??
 
Xpath functionality throught the use of the SelectNodes method is not
available in v1.0 of NETCF (however we will be supporting that in V2)
To read only certain tags from the file, you'll have to XmlTextReader to go
parse the entire xml and look for specific element type and name.
e.g

while(xtr.Read())
{
if(xtr.NodeType == XmlNodeType.CDATA)
{
//Do your processing here
break; // or continue; (Depending on ur logic)
}
}
In general you are encouraged to use the XmlTextReader rather than the DOM
as the reader is more performant.
See some more XML best practices here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/
scalenetchapt09.asp

I'll be glad to answer anymore questions you might have.
HTH,
Mark.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top