2012-10-04 78 views
2

我想獲得一個程序來連續將數據寫入文件中的XML節點,但我不知道如何實現它。 XML文件是需要以座標繪製路徑的谷歌地球KML文件:XML不斷更新值

<?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? 
+0

中還引用了這個文本文件你*有*使用XmlDocument的?是否使用LINQ to XML代替? –

+1

'node.InnerText = node.InnerText + myOtherText'? –

回答

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; 
     } 
    } 
} 
+0

謝謝ken,解決了問題! –

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> 
+0

我也會看看這個方法。謝謝奧利弗! –