XML不断更新值

时间:2012-10-04 15:34:40

标签: c# xml linq

我正在尝试让程序连续将数据写入文件中的XML节点,但我不确定如何实现它。 XML文件是Google Earth kml文件,需要坐标才能绘制路径:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Style>
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
    </Style>
    <Placemark>
      <LineString>
        <altitudeMode>clampToGround</altitudeMode>
        <coordinates>0,0,0</coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>

我只需要修改坐标节点并添加后续行。这将使用计时器每秒写一组新的坐标来完成。这可能有数百行:

<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

(...etc)

</coordinates>

我不太了解如何只访问坐标节点并在值之后写入值。我尝试过类似的东西,但它不起作用:

  XmlDocument dataFile = new XmlDocument();
  dataFile.Load("gpsData.kml");

  XmlNode node = dataFile.SelectSingleNode("kml/Document/Placemark/LineString/coordinates");

  node.InnerText (?) <- how do I append new text instead of replacing the whole thing?

2 个答案:

答案 0 :(得分:0)

刚刚鞭打了这个。不确定是否有更快的方法来完成同样的事情。但是对我有用。

using System;
using System.Timers;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {

            var fileName = "C:\\gpsData.kml";

            var xml = XDocument.Load(fileName);
            var xmlns = xml.Root.Name.Namespace;
            var document = xml.Root.Element(xmlns.GetName("Document"));
            var placemark = document.Element(xmlns.GetName("Placemark"));
            var lineString = placemark.Element(xmlns.GetName("LineString"));
            var coordinates = lineString.Element(xmlns.GetName("coordinates"));

            var inc = 0;
            var timer = new Timer();
            var timer_Elapsed = new ElapsedEventHandler((s, e) => {

                // get the new coordinate here
                var newCoordinate = "-112.2550785337791,36.07954952145647,2357";
                var oldValue = coordinates.Value;
                var newValue = oldValue + Environment.NewLine + newCoordinate;
                coordinates.Value = newValue;

                xml.Save(fileName);

                inc++;
            });

            timer.Interval = 1000;
            timer.Elapsed += timer_Elapsed;
            timer.Start();

            while (10 > inc) ;

            timer.Stop();
            timer.Elapsed -= timer_Elapsed;
        }
    }
}

答案 1 :(得分:0)

每次都必须重写整个XML文件。没有办法只更新XML文件的一部分。随着文件的增长,该过程将逐渐减慢。请考虑使用纯文本文件。

File.AppendAllText(path, newText);

或(从.NET 4.0开始)

File.AppendAllLines(path, stringEnumeration);

您也可以在XML文件中引用此文本文件

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:xlink="http://www.w3.org/1999/xlink">
  <Document>
    <Style>
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
    </Style>
    <Placemark>
      <LineString>
        <altitudeMode>clampToGround</altitudeMode>
        <coordinates xlink:href="http://mysite.com/data/coordinates.txt"/>
      </LineString>
    </Placemark>
  </Document>
</kml>