WP7 XML insert into GPX
-
03-07-2021 - |
Question
In my Isolated storage I have a file called Route.gpx which is getting created with this code:
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("Route.gpx", FileMode.Create, myStore))
{
XNamespace ns = "http://www.topografix.com/GPX/1/1";
XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";
XDocument xDoc = new XDocument(
new XDeclaration("1.0", "UTF-8", "no"),
new XElement(ns + "gpx",
new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
new XAttribute(xsiNs + "schemaLocation",
"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"),
new XAttribute("creator", "XML tester"),
new XAttribute("version", "1.1"),
new XElement(ns + "trk",
new XElement(ns + "trkseg",
new XElement(ns + "trkpt",
new XAttribute("lat", "7.0"),
new XAttribute("lon", "19.0"),
new XElement(ns + "time", DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture))
)))));
xDoc.Save(myStream);
But now I want to add an extra trkpt
element, I've tried with this code:
XDocument doc1;
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("Route.gpx", FileMode.Open, myStore))
{
doc1 = XDocument.Load(myStream);
}
var root = doc1.Element("trkseg");
var rows = root.Descendants("trkpt");
var lastRow = rows.Last();
lastRow.AddAfterSelf(
//XElement trkseg =
new XElement("trkpt",
new XElement("time", DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture))));
using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("Route.gpx", FileMode.Create, myStore))
{
doc1.Save(myStream);
}
which I got from here The code just catch an exception then pressed.
La solution
You're trying to access the "trkseg" element as a descendent of the root object while you're creating it at a level lower.
Try:
var root = doc1.Element("trk").Element("trkseg");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow