C# Xml反序列化器不';我的gpx文件不工作

C# Xml反序列化器不';我的gpx文件不工作,c#,xml,xml-deserialization,C#,Xml,Xml Deserialization,我试图将gpx文件反序列化到类中,但遇到了问题。我曾尝试将gpx文件文本输入Xml2CSharp并以这种方式生成一个类,但我不确定它是否正确()。当我使用调试器时,gpx为null。有人能告诉我我做错了什么吗?谢谢 Gpx文本: <gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.0"> <trk> <trkseg> <start lat="37.4899

我试图将gpx文件反序列化到类中,但遇到了问题。我曾尝试将gpx文件文本输入Xml2CSharp并以这种方式生成一个类,但我不确定它是否正确()。当我使用调试器时,gpx为null。有人能告诉我我做错了什么吗?谢谢

Gpx文本:

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

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

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; }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XmlSerializer XmlSerializer=新的XmlSerializer(typeof(XmlSerializeGpx.Gpx),”http://www.topografix.com/GPX/1/1");
FileStream fs=newfilestream(文件名,FileMode.Open);
XmlReader=XmlReader.Create(fs);
XmlSerializeGpx.Gpx gpxObj=(XmlSerializeGpx.Gpx)xmlSerializer.Deserialize(读取器);
}
}
公共类XMLGPX
{
[XmlRoot(ElementName=“start”,命名空间=”http://www.topografix.com/GPX/1/1")]
公开课开始
{
公共双元素{get;set;}
公共日期时间{get;set;}
[XmlAttribute(“lat”)]
公共双lat{get;set;}
[XmlAttribute(“lon”)]
公共双lon{get;set;}
}
[XmlRoot(ElementName=“trkpt”,命名空间=”http://www.topografix.com/GPX/1/1")]
公共类Trkpt
{
公共双元素{get;set;}
公共日期时间{get;set;}
[XmlAttribute(“lat”)]
公共双lat{get;set;}
[XmlAttribute(“lon”)]
公共双lon{get;set;}
}
[XmlRoot(ElementName=“trkseg”,命名空间=”http://www.topografix.com/GPX/1/1")]
公共类Trkseg
{
[XmlElement(“开始”)]
公共列表开始{get;set;}
[XmlElement(“trkpt”)]
公共列表trkpt{get;set;}
}
[XmlRoot(ElementName=“trk”,命名空间=”http://www.topografix.com/GPX/1/1")]
公共类Trk
{
公共Trkseg Trkseg{get;set;}
}
[XmlRoot(ElementName=“gpx”,命名空间=”http://www.topografix.com/GPX/1/1")]
公共类Gpx
{
公共Trk Trk{get;set;}
}
}
}

?是的,我尝试使用它将xml文件转换为c类。谢谢!类名的小写是个问题。但我也将反序列化转换为XmlSerializeGpx,而不是XmlSerializeGpx.Gpx。
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();
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; }
        }
    }
}