|
-
May 22nd, 2012, 02:41 PM
#1
Thread Starter
PowerPoster
[RESOLVED] XML Writer - Loop through treeview
Ola,
I have a treeview with a couple of rows + cells. How do I loop through the treeview so it creates the XML.
Code:
<myxml>
<name><%=mytreeview.cells(1).text %></name>
<description>A fixed description</description>
<myval>
<values><%=mytreeview.cells(2).text & ", " & <%=mytreeview.cells(3).text %></values>
</myval>
</myxml>
so it should create a new within the xml for each row.
Code:
<myxml>
<name>bla</name>
<description>A fixed description</description>
<myval>
<values>bla, bla</values>
</myval>
</myxml>
I hope someone understands what I'm trying to do.
Thanks in advance.
Last edited by Radjesh Klauke; May 22nd, 2012 at 02:44 PM.
-
May 22nd, 2012, 07:33 PM
#2
Re: XML Writer - Loop through treeview
I'm a little lost. A TreeView has no cells property.
However, I have an idea of what you want. The basic idea is generating XML from a list of strings:-
vbnet Code:
'
Dim names() As String = {"Adrian", "Spirit", "Anika", "Clare", "Dawn"}
Dim xml = <Root>
<Names><%= From N As String In names Select <name>
<%= N %>
</name> %>
</Names>
</Root>
The above code produces this XML:-
xml Code:
<Root>
<Names>
<name>Adrian</name>
<name>Spirit</name>
<name>Anika</name>
<name>Clare</name>
<name>Dawn</name>
</Names>
</Root>
Hopefully, you can adapt it to your example.
-
May 23rd, 2012, 02:24 AM
#3
Thread Starter
PowerPoster
Re: XML Writer - Loop through treeview
Ola Niya. thanks for the reply. I'm using a 3rd party control that does have that functionality. 
I think that I can make it work now. Will let you know.
-
May 23rd, 2012, 04:24 AM
#4
Thread Starter
PowerPoster
Re: XML Writer - Loop through treeview
Hmmm... I thought I had it... 
Okay. I'm trying to create a kml-file. An example of a kml file:
Code:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<folder>
<placemark>
<name>M.H.C. Olympia</name>
<point>
<coordinates>3.84197,51.31808,0</coordinates>
</point>
</placemark>
<placemark>
<name>M.H.C. Vlissingen</name>
<point>
<coordinates>3.56222,51.45759,0</coordinates>
</point>
</placemark>
</folder>
</kml>
my treeview looks like:

my partial code:
vb.net Code:
For Each x As Node In AdvTree1.Nodes Dim nName() As String = {x.Cells(0).Text} Dim nVal() As String = {x.Cells(1).Text} Dim nVal2() As String = {x.Cells(2).Text} Dim doc As XDocument = _ <?xml version="1.0" encoding="utf-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <folder> <placemark> <%= From n As String In nName Select<name><%= nName %></name>%> <description>blabla</description> <point><%= From o As String In nVal Select <coordinates><%= nVal %></coordinates>%> </point> </placemark> </folder> </kml> Next
This returns:
Code:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<folder>
<placemark>
<name xmlns="">Sexbierum</name>
<description>blabla</description>
<point>
<coordinates xmlns="">53.218837</coordinates>
</point>
</placemark>
</folder>
</kml><?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<folder>
<placemark>
<name xmlns="">New York (USA)</name>
<description>blabla</description>
<point>
<coordinates xmlns="">40.7143528</coordinates>
</point>
</placemark>
</folder>
</kml><?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<folder>
<placemark>
<name xmlns="">Dummy</name>
<description>blabla</description>
<point>
<coordinates xmlns="">31.500</coordinates>
</point>
</placemark>
</folder>
</kml>
If you compare it with an official you can see it's totally wrong. Besides that how do I add the nVal2 in the coordinates?
Last edited by Radjesh Klauke; May 23rd, 2012 at 04:32 AM.
-
May 23rd, 2012, 10:36 AM
#5
Re: XML Writer - Loop through treeview
It looks right. What exactly is wrong with what you got ? What is it supposed to look like ?
-
May 23rd, 2012, 10:55 AM
#6
Re: XML Writer - Loop through treeview
Try something along these lines:
vb.net Code:
Dim doc As XDocument = <?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<folder>
<%= From node In TreeView1.Nodes
Select <placemark>
<name><%= node.Cells(0).Text %></name>
<point>
<coordinates><%= String.Format("{0},{1},0",
node.Cells(1).Text,
node.Cells(2).Text) %></coordinates>
</point>
</placemark>
%>
</folder>
</kml>
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
May 23rd, 2012, 11:55 AM
#7
Thread Starter
PowerPoster
Re: XML Writer - Loop through treeview
@Niya. See the first codebock in #4. The code I tried created a complete new "<?xml version="1.0" encoding="utf-8"?>" - line. Thank you for all the help m8. (+REP)
Having 1 big issues with the code of Matt.
n.cells.... -> Option Strict doesn't allow late binding
When I add option strict off it works fine btw. Thanks you for that. Only this late binding needs to be resolved.
-
May 23rd, 2012, 12:25 PM
#8
Re: XML Writer - Loop through treeview
 Originally Posted by Radjesh Klauke
@Niya. See the first codebock in #4. The code I tried created a complete new "<?xml version="1.0" encoding="utf-8"?>" - line.
Oh....Then the code MattP posted is closer to your code should look like. I don't know anything about that TreeView so I can't really comment on the late binding issue, but it looks like you are in a much better position now.
-
May 23rd, 2012, 12:28 PM
#9
Re: XML Writer - Loop through treeview
From node In TreeNode1.Nodes is the problem as it's inferring node to be of type object. Change it to "node As SomeType In TreeView1.Nodes.Cast(Of SomeType)()" and it should solve the late binding problem.
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
May 23rd, 2012, 12:30 PM
#10
Thread Starter
PowerPoster
Re: XML Writer - Loop through treeview
You're the best!!! Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|