如何消除命名空间本地名称

时间:2019-05-03 18:52:00

标签: c# xml linq-to-xml

我正在尝试根据以下内容创建XML文档:

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

一切正常。但是,我在根标记中有问题。我想要一个默认的名称空间。但是,它使用本地名称。我已经尝试了各种解决方案,例如空字符串,无济于事。

        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())
        )));

我得到:

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

我想要的是:

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

0 个答案:

没有答案