GPX XML File Namespace without Prefix

S

Stuart Shay

Hello All:

I am trying to create a GPX File from my domain objects I am able to create
the file, but I can properly assign the default namespace to the xml

In the Code Below I trying to define the namespace as follows

<gpx version="1.1" creator="Garmin Connect"
xmlns="http://www.topografix.com/GPX/1/1"

How Do I Define a NS with out a prefix. I added a attribute to the Root but
then the Children got the namespace appended to all the Tags

Thanks
Stuart

/* Source Code */

XNamespace ns = "http://www.topografix.com/GPX/1/1";
XNamespace gpxtpx =
"http://www.garmin.com/xmlschemas/TrackPointExtension/v1";
XNamespace gpxx = "http://www.garmin.com/xmlschemas/GpxExtensions/v3";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace schemaLocation = "http://www.topografix.com/GPX/1/1
http://www.topografix.com/GPX/1/1/gpx.xsd
http://www.garmin.com/xmlschemas/GpxExtensions/v3
http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd
http://www.garmin.com/xmlschemas/TrackPointExtension/v1
http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd";

var xml = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("gpx",
new XAttribute("version", "1.1"),
new XAttribute("creator", "GPS Navigator"),
new XAttribute(xsi + "SchemaLocation",
schemaLocation.ToString()),
/****** Fomat
xmlns="http://www.topografix.com/GPX/1/1" *******/
new XAttribute(XNamespace.Xmlns + "NOPREFIX",
ns),
new XAttribute(XNamespace.Xmlns + "gpxtpx",
gpxtpx),
new XAttribute(XNamespace.Xmlns + "gpxx",
gpxx),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
new XElement("metadata", new XElement("time")),
from entry in
gpx.Trk.TrkSegments
select new XElement("trk",
new XElement("name",
entry.Name), new XElement("trkseg",
from t in entry.Trkpoints
select new XElement("trkpt",
new XAttribute("lon", t.Lon),new XAttribute("lat", t.Lat),
new XElement("ele", t.Ele)
,new XElement("time", t.Time))

)
)
));
 
M

Martin Honnen

Stuart said:
I am trying to create a GPX File from my domain objects I am able to
create the file, but I can properly assign the default namespace to the xml

In the Code Below I trying to define the namespace as follows

<gpx version="1.1" creator="Garmin Connect"
xmlns="http://www.topografix.com/GPX/1/1"

How Do I Define a NS with out a prefix. I added a attribute to the Root
but then the Children got the namespace appended to all the Tags

Use that XNamespace each time you want to create an element in that
namespace

so here you need
new XElement(ns + "gpx",
new XAttribute("version", "1.1"),
new XAttribute("creator", "GPS Navigator"),
new XAttribute(xsi + "SchemaLocation",
schemaLocation.ToString()),
/****** Fomat
xmlns="http://www.topografix.com/GPX/1/1" *******/
new XAttribute(XNamespace.Xmlns + "NOPREFIX",
ns),

drop that line
new XAttribute(XNamespace.Xmlns + "gpxtpx",
gpxtpx),
new XAttribute(XNamespace.Xmlns + "gpxx",
gpxx),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),

but here make sure you do
new XElement("metadata", new XElement("time")),

new XElement(ns + "gpx", new XElement(ns
+ "time")),

and so on each time you create an element node in the namespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top