Kabi
Kabi

Reputation: 2045

Error inserting XML data into database via LINQ

I want to use LINQ to insert some fields in a database. One of these fields contains XML data (GPX).

When I run the following code:

 rtlq.GpxData = new XElement(route.GpxData);

I get this exception:

Name cannot begin with the '<' character, hexadecimal value 0x3C

I would like to say route.GpxData has a type string and I converted this type to xml. Here is my whole code:

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

Would you please help me to solve this problem?

EDIT Sample of XML file:

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

Upvotes: 0

Views: 529

Answers (2)

Nacho
Nacho

Reputation: 962

Try

rtlq.GpxData = XElement.Parse(route.GpxData);

Upvotes: 1

Brett Zamir
Brett Zamir

Reputation: 14345

route.GpxData is presumably a string in XML. The XElement constructor accepting a string name will seek to convert it into an object representing an XML element. An XML element NAME cannot contain < characters, though `<' can be within content (if properly escaped).

Upvotes: 2

Related Questions