我正在尝试使用Linq的XDocument创建一个GPX序列化程序。在文档的开头,我应该指定3个XML命名空间,以实现这样的目的:
<?xml version='1.0' encoding='UTF-8' ?>
<gpx version="1.1" creator="TrekBuddy 0.9.99" xmlns="http://www.topografix.com/GPX/1/1" xmlns:nmea="http://trekbuddy.net/2009/01/gpx/nmea" xmlns:gsm="http://trekbuddy.net/2009/01/gpx/gsm">
<trk>
<trkseg>
<trkpt lat="48.926513616" lon="19.650097458">
<ele>1787.0</ele>
<time>2010-08-12T13:47:17.214Z</time>
<fix>3d</fix>
<sat>6</sat>
<extensions>
<nmea:course>263.8</nmea:course>
<nmea:speed>1.2</nmea:speed>
</extensions>
</trkpt>
我正在使用以下代码进行XDocument实例化:
private XDocument CreateXDocument(Gpx.Gpx data)
{
XNamespace xmlns = data.XmlnsUrl;
XNamespace nmea = data.NmeaUrl;
XNamespace gsm = data.GsmUrl;
return new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement(xmlns + "gpx",
new XAttribute("creator", data.Creator),
new XAttribute(nmea + "nmea", data.NmeaUrl),
new XAttribute(gsm + "gsm", data.GsmUrl),
CreateTrk(data.Track, nmea)));
}
此代码用于添加单个扩展程序:
private XElement CreateExtension(Extension extension, XNamespace nmeaNamespace)
{
if (extension is NmeaExtension)
{
var nmea = extension as NmeaExtension;
return new XElement("extensions",
new XElement(nmea + "course", nmea.Course),
new XElement(nmea + "speed", nmea.Speed));
}
else return new XElement("extensions");
}
但是,我得到以下结果,而不是预期的结果:
<gpx creator="TrekBuddy 0.9.99" p1:nmea="http://trekbuddy.net/2009/01/gpx/nmea" p2:gsm="http://trekbuddy.net/2009/01/gpx/gsm" xmlns:p2="http://trekbuddy.net/2009/01/gpx/gsm" xmlns:p1="http://trekbuddy.net/2009/01/gpx/nmea" xmlns="http://www.topografix.com/GPX/1/1">
<trk xmlns="">
<trkseg>
<trkpt lat="4909.933" lon="2016.67883">
<ele>1787.0</ele>
<time>2010-08-12T13:47:17.214Z</time>
<fix>3d</fix>
<sat>6</sat>
<extensions>
<Geolocation.Gpx.Extensions.NmeaExtensioncourse>188.6</Geolocation.Gpx.Extensions.NmeaExtensioncourse>
<Geolocation.Gpx.Extensions.NmeaExtensionspeed>0.9774523</Geolocation.Gpx.Extensions.NmeaExtensionspeed>
</extensions>
</trkpt>
有人可以帮我调整我的代码,所以我得到了预期的结果吗? official documentation在这种情况下并不特别有用......