【问题标题】:Adding elements to a XML child将元素添加到 XML 子项
【发布时间】:2020-11-08 17:47:30
【问题描述】:

我是 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>......

如何获得 'trkseg' 元素的句柄以便可以添加到它?

【问题讨论】:

  • 您不应该添加到新的 XElement("trkseg") 中吗?所以你应该定义一个变量来保存这个元素,并在你需要它的任何地方引用它。

标签: c# linq-to-xml


【解决方案1】:

以下是如何将跟踪点放入 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; }
    }
}

这里的输出:


<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.

【讨论】:

  • 完美,谢谢。必须创建新对象来测试代码示例的额外积分。全部在 15 分钟内完成。
  • 很高兴我能帮上忙
猜你喜欢
  • 2013-07-19
  • 2017-10-22
  • 2018-04-28
  • 1970-01-01
  • 2019-10-14
  • 2012-06-14
  • 2023-03-26
  • 2012-04-14
  • 1970-01-01
相关资源
最近更新 更多