Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WP7XML插入到GPX中_C#_Xml_Windows Phone 7_Gpx - Fatal编程技术网

C# WP7XML插入到GPX中

C# WP7XML插入到GPX中,c#,xml,windows-phone-7,gpx,C#,Xml,Windows Phone 7,Gpx,在我的独立存储中,我有一个名为Route.gpx的文件,该文件是使用以下代码创建的: using (IsolatedStorageFileStream myStream = new IsolatedStorageFileStream("Route.gpx", FileMode.Create, myStore)) { XNamespace ns = "http://www.topografix.com/GPX/1/1"; XNamespace xsiNs = "http://www

在我的独立存储中,我有一个名为Route.gpx的文件,该文件是使用以下代码创建的:

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);
但现在我想添加一个额外的trkpt元素,我已经尝试使用以下代码:

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);
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);
}
这是我从
代码只是捕获了一个异常,然后按下。

当您在较低级别创建trkseg元素时,您试图将其作为根对象的后代来访问

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);
尝试:

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);

变量行中的NullReferenceException=root.DegenantsTrkpt;,DistributeRetentKnologi12\u GPSPhonePano.dll中发生了类型为“System.NullReferenceException”的第一次意外异常。我认为这是因为它找不到trkseg,但我不确定。仍然捕获异常NullReferenceException未处理,在新行中。我也尝试过:var root=doc1.Elementgpx.Elementtrk.Elementtrkseg;因为我有一个名称空间为gpx的元素,但我仍然捕获到一个异常。我尝试删除元素gpx,然后使用您的代码,现在它可以工作了。但是我需要GPX元素。在尝试获取GPX元素时,需要包含名称空间限定。