0

I am trying to create an XML doc according to the following:

https://docs.safe.com/fme/html/FME_Desktop_Documentation/FME_ReadersWriters/gpxx/Feature_Types.htm

Just about everything works. However, I am having a problem in the root tag. I want a default namespace. However, it puts in a local name. I have tried various solutions, e.g. empty string, to no avail.

        XNamespace def = @"http://www.topografix.com/GPX/1/1";
        XNamespace gpxx = @"http://www.garmin.com/xmlschemas/GpxExtensions/v3";
        XNamespace xsi = @"http://www.w3.org/2001/XMLSchema-instance";

        XDocument doc = new XDocument(
        new XDeclaration("1.0", "utf-8", "no"),
        new XElement("gpx",
            new XAttribute(XNamespace.Xmlns + "def", def),
            new XAttribute(XNamespace.Xmlns + "gpxx", gpxx),
            new XAttribute(XNamespace.Xmlns + "xsi", xsi),
            new XAttribute (xsi + "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://www8.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd"),
            new XElement("metadata",
            new XElement("time", DateTime.Now.ToUniversalTime().ToString())
        )));

I get:

<gpx xmlns:def="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" 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.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd"> <metadata> <time>2019-05-03 18:46:27</time> </metadata>

What I want is:

<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" 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.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd"> <metadata> <time>2019-05-03 18:46:27</time> </metadata>

DrTom
  • 29
  • 1
  • 7
  • The net library wants the default namespace at the end of the list. I usually just create a string of the root node and then use XDocument.Parse(string) so I don't have to deal with the Syntax. The code is much easier to read. – jdweng May 03 '19 at 19:42
  • Thanx. That did the trick. I just wonder why they can't handle default namespaces gracefully. – DrTom May 03 '19 at 21:01
  • Do not know why Microsoft does what they do. – jdweng May 04 '19 at 08:57

0 Answers0