0

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>
8
  • Should it be XmlDocument instead of XElement ? Commented Jul 3, 2012 at 7:11
  • Could you post a sample of the route.GpxData's value Commented Jul 3, 2012 at 7:13
  • 1
    and it should load xml. using XmlDocument doc = new XmlDocument(); doc.LoadXml(route.GpxData); Commented Jul 3, 2012 at 7:13
  • 1
    IF you still want to use XElement then remember that constructor overload you are using expects element name whereas I think you want to provide the data not name so use overload new XElement(name,route.GpxData) Commented Jul 3, 2012 at 7:14
  • 1
    This is because rtlq.GpxData is of type XElement whereas doc is of type XmlDocument. In that case as Marc suggested use XElement.Parse. Commented Jul 3, 2012 at 7:29

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Indeed; in which case, replacing with the line rtlq.GpxData = XElement.Parse(route.GpxData); should fix it
Hey Marc,I think my problem has been solved,I don't see this error but another error and I think I should post another Question,How can I mark your answer as ANSWER
I am still getting this error, but no name attributes in my file start with <
1

Try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.