我想使用 LINQ 在数据库中插入一些字段。这些字段之一包含 XML 数据 (GPX)。
当我运行以下代码时:
rtlq.GpxData = new XElement(route.GpxData);
我得到这个例外:
名称不能以
'<'
字符开头,十六进制值0x3C
我想说route.GpxData
有一个类型字符串,我将此类型转换为 xml。这是我的整个代码:
public int Save(Route route)
{
aspnetdbDataContext aspdb = new aspnetdbDataContext();
RouteLinq rtlq=new RouteLinq();
rtlq.UserId = route.UserId;
rtlq.SourceName = route.Name;
//I have an error here
rtlq.GpxData = new XElement(route.GpxData);
rtlq.CreationTime = route.Time;
aspdb.RouteLinqs.InsertOnSubmit(rtlq);
aspdb.SubmitChanges();
int k1;
System.Data.Linq.ChangeSet cs1 = aspdb.GetChangeSet();
k1=cs1.Inserts.Count();
TrackPointlinq trlq = new TrackPointlinq();
foreach (var trackpoint in route.TrackPoints)
{
trlq.RouteFK=route.Id;
trlq.TrackTime=trackpoint.Time;
trlq.Latitude=(float) trackpoint.Latitude;
trlq.Longitude=(float)trackpoint.Longitude;
trlq.Elevation=trackpoint.Elevation;
}
aspdb.TrackPointlinqs.InsertOnSubmit(trlq);
aspdb.SubmitChanges();
int k2;
System.Data.Linq.ChangeSet cs2 = aspdb.GetChangeSet();
k2 = cs2.Inserts.Count();
if ((k1 == 0) && (k2 == 0))
{
return 1;
}
else
return 0;
}
你能帮我解决这个问题吗?
编辑 XML 文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpsies="http://www.gpsies.com/GPX/1/0" creator="GPSies http://www.gpsies.com - GpsiesTrack" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.gpsies.com/GPX/1/0 http://www.gpsies.com/gpsies.xsd">
<metadata>
<name>GpsiesTrack</name>
<link href="http://www.gpsies.com/">
<text>GpsiesTrack on GPSies.com</text>
</link>
<time>2012-06-26T18:58:39Z</time>
</metadata>
<trk>
<name>GpsiesTrack on GPSies.com</name>
<trkseg>
<trkpt lat="50.81482934" lon="12.90653228">
<ele>305.00000</ele>
<time>2012-05-01T00:00:00Z</time>
</trkpt>
<trkpt lat="50.85364209" lon="12.92404174">
<ele>297.00000</ele>
<time>2012-05-01T00:15:27Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>