Xml 解串器不适用于我的 gpx 文件
Xml Deserializer doesn't work for my gpx file
我正在尝试将 gpx 文件反序列化为 class,但我遇到了问题。我尝试将 gpx 文件文本输入 Xml2CSharp 并以这种方式生成 class,但我不确定它是否正确 (http://xmltocsharp.azurewebsites.net/)。当我使用调试器时,gpx 为空。有人可以让我了解我做错了什么吗?谢谢。
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>
当前 class:
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();
您需要正确输入大小写字母。请参阅下面的测试代码
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; }
}
}
}
我正在尝试将 gpx 文件反序列化为 class,但我遇到了问题。我尝试将 gpx 文件文本输入 Xml2CSharp 并以这种方式生成 class,但我不确定它是否正确 (http://xmltocsharp.azurewebsites.net/)。当我使用调试器时,gpx 为空。有人可以让我了解我做错了什么吗?谢谢。
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>
当前 class:
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();
您需要正确输入大小写字母。请参阅下面的测试代码
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; }
}
}
}