appending to an xml file in c#

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
6,981
Location
Manchester
Hi.

using this as an example;

Code:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>Paths</name>
    <description>Examples of paths. Note that the tessellate tag is by default
      set to 0. If you want to create tessellated lines, they must be authored
      (or edited) directly in KML.</description>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Placemark>
      <name>Absolute Extruded</name>
      <description>Transparent green wall with yellow outlines</description>
      <styleUrl>#yellowLineGreenPoly</styleUrl>
      <LineString>
        <extrude>1</extrude>
        <tessellate>1</tessellate>
        <altitudeMode>absolute</altitudeMode>
        <coordinates> -112.2550785337791,36.07954952145647,2357
          -112.2549277039738,36.08117083492122,2357
          -112.2552505069063,36.08260761307279,2357
          -112.2564540158376,36.08395660588506,2357
          -112.2580238976449,36.08511401044813,2357
          -112.2595218489022,36.08584355239394,2357
          -112.2608216347552,36.08612634548589,2357
          -112.262073428656,36.08626019085147,2357
          -112.2633204928495,36.08621519860091,2357
          -112.2644963846444,36.08627897945274,2357
          -112.2656969554589,36.08649599090644,2357 
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>

how, using c# would you go about adding more coordinates to that list. I have been searching and pondering, and not really come up with much useful. I can add new elements etc, but not able to append to existing.

I may just be having a blonde moment mind.
 
Code:
XmlDocument doc = new XmlDocument();
doc.LoadXml(theXmlAboveAsString); // use Load() instead for Stream or Reader
XmlNodeList nodes = doc.SelectNodes("/Document/Placemark/LineString/coordinates");
foreach (XmlNode node in nodes)
{
  node.InnerText.Append(yourNewCoordinateString);
}
or similar.. I don't have VS installed at the moment to actually check/test :)
 
I would load it into an XmlDocument and add to it like (99% sure you can add elements etc) that followed by overwriting the file you loaded it from.
Might be an even slicker way with linq2xml however?

edit: No one likes a smart a55 jestar :p
 
You could use LINQ to query the XML tags. Generally you could just read in the value of <coordinates> to a local variable then append new data to it. then write it back to the attribute.

Any reason as to why all the coordinates are within the 1 tag. Would it not be more logical to have each coordinate seperate or in pairs, or threes?
 
thanks guys, it just didn't seem right in my head loading up the whole file just to append to it - and LINQ is a new thing to me. I'll look into that now.

Any reason as to why all the coordinates are within the 1 tag. Would it not be more logical to have each coordinate seperate or in pairs, or threes?

blame google, that's an example KML file.
 
Back
Top Bottom