Xml反序列化器对我的gpx文件

时间:2017-11-08 19:55:18

标签: c# xml xml-deserialization

我正在尝试将gpx文件反序列化为类,但我遇到了麻烦。我已经尝试将gpx文件文本输入Xml2CSharp并以此方式生成类,但我不确定它是否正确(http://xmltocsharp.azurewebsites.net/)。当我使用调试器时,gpx为null。有人能给我一些关于我做错了什么的见解吗?感谢。

Gpx Text:

<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.0">
   <trk>
      <trkseg>
         <start lat="37.48996833333333" lon="-122.20991333333335">
            <ele>127.1</ele>
            <time>2017-11-07T02:53:07Z</time>
         </start>
         <trkpt lat="37.48996833333333" lon="-122.20991333333335">
            <ele>127.1</ele>
            <time>2017-11-07T02:53:07Z</time>
         </trkpt>
         <trkpt lat="37.48996833333333" lon="-122.20991333333335">
            <ele>127.1</ele>
            <time>2017-11-07T02:53:07Z</time>
         </trkpt>
      </trkseg>
   </trk>
</gpx>

当前课程:

public class XmlSerializeGpx
{
    public Gpx gpx { get; set; }

    public class Start
    {
        public string Ele { get; set; }
        public string Time { get; set; }
        public string Lat { get; set; }
        public string Lon { get; set; }
    }

    public class Trkpt
    {
        public string Ele { get; set; }
        public string Time { get; set; }
        public string Lat { get; set; }
        public string Lon { get; set; }
    }

    public class Trkseg
    {
        public Start Start { get; set; }
        public List<Trkpt> Trkpt { get; set; }
    }

    public class Trk
    {
        public Trkseg Trkseg { get; set; }
    }

    public class Gpx
    {
        public Trk Trk { get; set; }
        public string Xmlns { get; set; }
        public string Version { get; set; }
    }
}

这是我的解串器:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "gpx";
xRoot.Namespace = gpxNs.NamespaceName;
xRoot.IsNullable = true;

XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlSerializeGpx),xRoot);
FileStream fs = new FileStream(file.Path, FileMode.Open);
XmlReader reader = XmlReader.Create(fs);

XmlSerializeGpx gpxObj;

gpxObj = (XmlSerializeGpx)xmlSerializer.Deserialize(reader);

fs.Close();

1 个答案:

答案 0 :(得分:1)

您需要获得正确的大写和小写字母。请参阅下面的测试代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(XmlSerializeGpx.Gpx), "http://www.topografix.com/GPX/1/1");

            FileStream fs = new FileStream(FILENAME, FileMode.Open);
            XmlReader reader = XmlReader.Create(fs);

            XmlSerializeGpx.Gpx gpxObj = (XmlSerializeGpx.Gpx)xmlSerializer.Deserialize(reader);

        }
    }
    public class XmlSerializeGpx
    {

        [XmlRoot(ElementName = "start", Namespace = "http://www.topografix.com/GPX/1/1")]
        public class Start
        {
            public double ele { get; set; }
            public DateTime time { get; set; }
            [XmlAttribute("lat")]
            public double lat { get; set; }
            [XmlAttribute("lon")]
            public double lon { get; set; }
        }

        [XmlRoot(ElementName = "trkpt", Namespace = "http://www.topografix.com/GPX/1/1")]
        public class Trkpt
        {
            public double ele { get; set; }
            public DateTime time { get; set; }
            [XmlAttribute("lat")]
            public double lat { get; set; }
            [XmlAttribute("lon")]
            public double lon { get; set; }
        }

        [XmlRoot(ElementName = "trkseg", Namespace = "http://www.topografix.com/GPX/1/1")]
        public class Trkseg
        {
            [XmlElement("start")]
            public List<Start> start { get; set; }
            [XmlElement("trkpt")]
            public List<Trkpt> trkpt { get; set; }
        }

        [XmlRoot(ElementName = "trk", Namespace = "http://www.topografix.com/GPX/1/1")]
        public class Trk
        {
            public Trkseg trkseg { get; set; }
        }

        [XmlRoot(ElementName = "gpx", Namespace = "http://www.topografix.com/GPX/1/1")]
        public class Gpx
        {
            public Trk trk { get; set; }
        }
    }
}