[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.
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.
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.
Re: XML Writer - Loop through treeview
Hmmm... I thought I had it... :cry:
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:
http://i50.tinypic.com/2nlqmmt.png
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?
Re: XML Writer - Loop through treeview
It looks right. What exactly is wrong with what you got ? What is it supposed to look like ?
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>
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.
Re: XML Writer - Loop through treeview
Quote:
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. :)
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.
Re: XML Writer - Loop through treeview
You're the best!!! Thanks. :thumb: