Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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# 向XML子级添加元素_C#_Linq To Xml - Fatal编程技术网

C# 向XML子级添加元素

C# 向XML子级添加元素,c#,linq-to-xml,C#,Linq To Xml,我不熟悉XML.Linq,不知道如何创建所需的XML输出。我快到了,但数据出现在错误的树结构中: static void Main(string[] args) { XElement GPX = new XElement("gpx", new XAttribute("version", "1.0"), new XAttribute("creator

我不熟悉XML.Linq,不知道如何创建所需的XML输出。我快到了,但数据出现在错误的树结构中:

    static void Main(string[] args)
    {
        XElement GPX = new XElement("gpx",
            new XAttribute("version", "1.0"),
            new XAttribute("creator", "quilkin.com"),
            new XElement("trk",
                new XElement("name","to_do"),
                new XElement("trkseg")
            )
        );
                    
        Track track = GarminTrack.ParseTCX("../../App_Data/25Oct_G2a.tcx");
        int count = 0;

        foreach (TrackPoint tp in track.TrackPoints)
        {
            Position pos = tp.Positionx[0];
            GPX.Add(new XElement("trkpt",
                new XAttribute("lat", pos.LatitudeDegrees),
                new XAttribute("lon", pos.LongitudeDegrees),
                new XElement("ele", tp.AltitudeMeters)));

            count++;
        }
        Console.WriteLine(GPX);
        Console.WriteLine(String.Format("{0} Track Points Processed.", count));
        Console.ReadKey();
    }
输出将所有“trkpt”元素添加到根“gpx”,而不是“trkseg”元素

<gpx version="1.0" creator="quilkin.com">
  <trk>
    <name>to_do</name>
    <trkseg />
  </trk>
  <trkpt lat="50.262084" lon="-5.0499">
  <ele>7</ele>
  </trkpt>
  <trkpt lat="50.262492" lon="-5.051214">
  <ele>7</ele>
  </trkpt>
  <trkpt lat="50.261889" lon="-5.051892">
  <ele>7</ele>
  </trkpt>......

做
7.
7.
7.
......

如何获取“trkseg”元素的句柄以便添加到其中?

以下是如何将轨迹点添加到trkseg元素中的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace XMLAdd
{
    class Program
    {
        static List<TrackPoint> trackpoints = new List<TrackPoint>
        {
            new TrackPoint
            {
                AltitudeMeters = 100,
                Positionx = new Position[] { new Position { LatitudeDegrees = 200, LongitudeDegrees = 200} }
            }
        };

        static void Main(string[] args)
        {
            // Save the trkseg element in a variable
            var trkseg = new XElement("trkseg");
            XElement GPX = new XElement("gpx",
                new XAttribute("version", "1.0"),
                new XAttribute("creator", "quilkin.com"),
                new XElement("trk",
                    new XElement("name", "to_do"),
                    trkseg // <-- reference it here
                )
            );
            int count = 0;

            foreach (TrackPoint tp in trackpoints)
            {
                Position pos = tp.Positionx[0];
                trkseg.Add(new XElement("trkpt",  // <-- and here
                    new XAttribute("lat", pos.LatitudeDegrees),
                    new XAttribute("lon", pos.LongitudeDegrees),
                    new XElement("ele", tp.AltitudeMeters)));

                count++;
            }
            Console.WriteLine(GPX);
            Console.WriteLine(String.Format("{0} Track Points Processed.", count));
            Console.ReadKey();
        }
    }

    public class TrackPoint
    {
        public object AltitudeMeters { get; internal set; }
        public Position[] Positionx { get; internal set; }
    }

    public class Position
    {
        public int LatitudeDegrees { get; internal set; }
        public object LongitudeDegrees { get; internal set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Xml.Linq;
名称空间XMLAdd
{
班级计划
{
静态列表跟踪点=新列表
{
新跟踪点
{
高度表=100,
位置X=新位置[]{新位置{相对坐标=200,纵向坐标=200}
}
};
静态void Main(字符串[]参数)
{
//将trkseg元素保存在变量中
var trkseg=新的元素(“trkseg”);
XElement GPX=新XElement(“GPX”,
新XAttribute(“版本”、“1.0”),
新XAttribute(“创建者”,“quilkin.com”),
新XElement(“trk”,
新条款(“名称”、“待办事项”),

trkseg//您不应该添加到新的XElement(“trkseg”)中吗?因此您应该定义一个变量来保存此元素,并在需要的任何地方引用它。非常好,谢谢。额外的要点是必须创建新对象来测试代码示例。所有这些都需要15分钟。很高兴我能提供帮助
<gpx version="1.0" creator="quilkin.com">
  <trk>
    <name>to_do</name>
    <trkseg>
      <trkpt lat="200" lon="200">
        <ele>100</ele>
      </trkpt>
    </trkseg>
  </trk>
</gpx>
1 Track Points Processed.