Answered by:
Append Value to XML Element

Question
-
Ok, so I have an XML file set up and one of the elements is <coordinates>
I want to pass in values as received by my GPS receiver into this element to build up a linestring in google earth.
How do I access the node <coordinates> and then append data to it?
I initially came up with the following, but think its way off base...
Public Sub AddToKmlLog(ByVal TrackFilePath As String, ByVal lat As String, ByVal lon As String) ' load the kml file thisFile = TrackFilePath Dim doc As XDocument = XDocument.Parse(thisFile) Dim element = doc.Root.Element("coordinates") element.Add(lon & "," & lat & ",0") doc.Save(thisFile) End Sub
Any help appreciated, I'm sure this is easy, just never had to do it before! :-)
Thursday, May 16, 2013 1:10 PM
Answers
-
String builder could be the answer I was looking for though..its just how to add the string at the correct point in the file each time...
Well what you could do would be to use a text editor (notepad) and copy the lines UP TO where the next line starts at <coordinates>.
See how it would work from that point on?
One good thing about a StringBuilder is that unlike a string, it's mutable, so use the built-in methods (.Append) to form the final output, then it's up to you what you do with that output.
Please call me Frank :)
- Marked as answer by bigcdh Thursday, May 16, 2013 4:12 PM
Thursday, May 16, 2013 3:57 PM
All replies
-
What you're asking for is a bit confusing.
You have an XML file that you're reading and you also want to write to it at the same time? Obviously that won't work.
Explain a bit better please? Can you post the XML file?
Please call me Frank :)
Thursday, May 16, 2013 3:06 PM -
Hi Frank,
The XML file is as below (actually, its KML for Google Earths purposes, but the same principle). Google Earth runs a network link on the file so refreshes the line path in Google Earth every 1 second or so. I'm taking the GPS feed from a receiver and looking to add to it each time I get a new GPS string on the comport, hence I'm writing to the file, Google Earth is reading it.
The file contents as follows:
<?xml version="1.0" encoding="utf-8"?> <Document> <name>Path</name> <Placemark> <Style> <LineStyle> <color>ff00ffff</color> <width>3</width> </LineStyle> </Style> <LineString> <extrude>0</extrude> <tessellate>0</tessellate> <coordinates> coordinates to be appended here more coordinates appended here even more appended here </coordinates> </LineString> </Placemark> </Document>
Cheers
Thursday, May 16, 2013 3:09 PM -
For reading it, you can do this (if it's a file):
Option Strict On Option Explicit On ' Imports System.IO.Path ' Public Class Form1 ' ' Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load ' Dim dt As String = My.Computer.FileSystem.SpecialDirectories.Desktop Dim xFilePath As String = Combine(dt, "Example.xml") ReadXML(xFilePath) ' End Sub ' ' Private Sub ReadXML(ByVal xFilePath As String) ' If My.Computer.FileSystem.FileExists(xFilePath) Then Dim gpsInfo = XElement.Load(xFilePath) ' For Each lsInfo As XElement In gpsInfo...<LineString> Dim coords As String = lsInfo...<coordinates>.Value Stop Next End If ' End Sub ' ' End Class
If it's in memory, use the .Parse method instead of .Load then everything is the same.
After that though, I'm confused on what you want to do. You can't read AND write to it - you'd need to create a new XML file, or I would think so.
Please call me Frank :)
Thursday, May 16, 2013 3:23 PM -
Hi Frank, thanks for the reply and code.
Think I may have confused matters.
Forget the reading / writing part, I'm really looking for the syntax to open an existing xml file and add a value at a given node in the tree. I can do it where I create a new file each time, but thats not what I'm after, its being able to append the coordinates to the coordinates node.
Cheers
Thursday, May 16, 2013 3:38 PM -
Hi Frank, thanks for the reply and code.
Think I may have confused matters.
Forget the reading / writing part, I'm really looking for the syntax to open an existing xml file and add a value at a given node in the tree. I can do it where I create a new file each time, but thats not what I'm after, its being able to append the coordinates to the coordinates node.
Cheers
Well honestly I don't know how to do that. In my mind, if it's being read then it cannot also be written to, but maybe I'm not following the logic.
Hopefully others here can help though.
Please call me Frank :)
Thursday, May 16, 2013 3:41 PM -
Hello,
The following is done with the xml inline but can also be done via XDocument.Load("Filename")
Dim MyDoc = <Document> <name>Path</name> <Placemark> <Style> <LineStyle> <color>ff00ffff</color> <width>3</width> </LineStyle> </Style> <LineString> <extrude>0</extrude> <tessellate>0</tessellate> <coordinates> <coordinate>111</coordinate> </coordinates> </LineString> </Placemark> </Document> MyDoc...<coordinates>(0).Add(<coordinate>222</coordinate>) Console.WriteLine(MyDoc.ToString)
Output from Console.WriteLine
<Document> <name>Path</name> <Placemark> <Style> <LineStyle> <color>ff00ffff</color> <width>3</width> </LineStyle> </Style> <LineString> <extrude>0</extrude> <tessellate>0</tessellate> <coordinates> <coordinate>111</coordinate> <coordinate>222</coordinate> </coordinates> </LineString> </Placemark> </Document>
If done from XDocument simply save back to disk.Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Thursday, May 16, 2013 3:46 PM -
Kevin,
You're making the assumption that all of the other stuff is always the same. Heck if that's the case, a StringBuilder could be used.
Also it was my understanding that the coord pair was to be a comma-seperated value of <coordinates> but I might be wrong about that.
Please call me Frank :)
Thursday, May 16, 2013 3:50 PM -
Kevin,
You're making the assumption that all of the other stuff is always the same. Heck if that's the case, a StringBuilder could be used.
Also it was my understanding that the coord pair was to be a comma-seperated value of <coordinates> but I might be wrong about that.
Please call me Frank :)
Hi Frank,
All the other stuff will actually be the same, its just the coordinates values that will alter, i.e. be appended to. So over time it just builds up a list of coordinates as values in the <coordinates> element. And correct, these are csv's
String builder could be the answer I was looking for though..its just how to add the string at the correct point in the file each time...
- Edited by bigcdh Thursday, May 16, 2013 3:55 PM
Thursday, May 16, 2013 3:52 PM -
String builder could be the answer I was looking for though..its just how to add the string at the correct point in the file each time...
Well what you could do would be to use a text editor (notepad) and copy the lines UP TO where the next line starts at <coordinates>.
See how it would work from that point on?
One good thing about a StringBuilder is that unlike a string, it's mutable, so use the built-in methods (.Append) to form the final output, then it's up to you what you do with that output.
Please call me Frank :)
- Marked as answer by bigcdh Thursday, May 16, 2013 4:12 PM
Thursday, May 16, 2013 3:57 PM -
Frank,
Yes you are correct, I am assuming :-) Same as below
Dim MyDoc = <Document> <name>Path</name> <Placemark> <Style> <LineStyle> <color>ff00ffff</color> <width>3</width> </LineStyle> </Style> <LineString> <extrude>0</extrude> <tessellate>0</tessellate> <coordinates> <coordinate>111</coordinate> <coordinate>BBB</coordinate> <coordinate>333</coordinate> </coordinates> </LineString> </Placemark> </Document> Dim Coords = (From T In MyDoc...<coordinate> Select T.Value).ToList For Each c In Coords Console.WriteLine(c) Next Console.WriteLine() Console.WriteLine((From T In MyDoc...<coordinate> Select T.Value).FirstOrDefault)
111 BBB 333 ..... 111
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Proposed as answer by Frank L. Smith Thursday, May 16, 2013 4:02 PM
Thursday, May 16, 2013 3:57 PM -
Kev,
You're still not getting what he want. ;-)
From his original post, he showed this:
element.Add(lon & "," & lat & ",0")
Other than that though, your method should work. :)
Please call me Frank :)
Thursday, May 16, 2013 4:02 PM -
Thanks Frank, I like the sb approach, much obliged both appreciate the efforts.
Thursday, May 16, 2013 4:12 PM -
Thanks Frank, I like the sb approach, much obliged both appreciate the efforts.
I'm glad it helped. It seems almost like a 'cheat' given we're dealing with XML, but whatever works!
Please call me Frank :)
Thursday, May 16, 2013 4:18 PM -
Kev,
You're still not getting what he want. ;-)
From his original post, he showed this:
element.Add(lon & "," & lat & ",0")
Other than that though, your method should work. :)
Please call me Frank :)
Same here, that is why I took a shot at it. No different from going in another direction as shown below
Dim MyDoc = <Document> <name>Path</name> <Placemark> <Style> <LineStyle> <color>ff00ffff</color> <width>3</width> </LineStyle> </Style> <LineString> <extrude>0</extrude> <tessellate>0</tessellate> <coordinates> <coordinate>111</coordinate> <coordinate>BBB</coordinate> <coordinate>333</coordinate> </coordinates> </LineString> </Placemark> </Document> Console.WriteLine( String.Join(" ", ( From T In MyDoc...<coordinate> Select T.Value).ToArray ) )
Produces
111 BBB 333
Or even this
Dim MyDoc = <Document> <name>Path</name> <Placemark> <Style> <LineStyle> <color>ff00ffff</color> <width>3</width> </LineStyle> </Style> <LineString> <extrude>0</extrude> <tessellate>0</tessellate> <coordinates> <coordinate>111</coordinate> <coordinate>BBB</coordinate> <coordinate>333</coordinate> </coordinates> </LineString> </Placemark> </Document> Console.WriteLine( String.Join("", ( From T In MyDoc...<LineString> From X In MyDoc...<coordinate> Select T.<extrude>.Value & " " & T.<tessellate>.Value & " " & X.Value & Environment.NewLine ).ToArray ) )
Output
0 0 111 0 0 BBB 0 0 333
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Edited by KareninstructorMVP Thursday, May 16, 2013 4:43 PM Added another example
Thursday, May 16, 2013 4:35 PM