1

我正在研究一个通过在 kml 中的特定位置添加 gps 坐标来修改 kml 文件的项目。但是,我的代码在最后保存时在所有元素上添加了“gx”命名空间。我研究并查看了大量不同的 Visual Basic XML 编写方法,但我已经没有想法了。如何停止添加不必要的命名空间?!

测试.kml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.opengis.net/kml/2.2">
  <Document>
    <name>test.kml</name>
    <Snippet>File Created Mon Jul 9 15:50:16 2012</Snippet>
    <Style id="multiTrack_n">
      <IconStyle>
        <Icon>
          <href>http://maps.google.com/mapfiles/kml/shapes/track.png</href>
        </Icon>
      </IconStyle>
      <LineStyle>
        <color>99ffac59</color>
        <width>6</width>
      </LineStyle>
    </Style>
    <Folder>
      <Placemark>
        <name>test.kml</name>
        <styleUrl>#multiTrack_n</styleUrl>
        <gx:Track>
          <!--GPS Tracking data Points-->
        </gx:Track>
      </Placemark>
    </Folder>
  </Document>
</kml>

代码:

Imports System.IO
Imports System.Xml
Imports System.DateTime
Imports <xmlns="http://www.opengis.net/kml/2.2">
Imports <xmlns:gx="http://www.opengis.net/kml/2.2">
...
Public Sub addCoordinate(ByVal lon As Double, ByVal lat As Double, ByVal att As Double, ByVal timeStamp As String)
        Dim currentDoc = XDocument.Load("test.kml")
        Try
            whenElement = _
                <when><%= timeStamp %></when>
            coordElement = _
                <gx:coord><%= lon.ToString %>,<%= lat.ToString %>,<%= att.ToString %></gx:coord>
            Dim testLocation = currentDoc.<kml>.<Document>.<Folder>.<Placemark>.Elements.Last()
            testLocation.Add(whenElement)
            testlocation.Add(coordElement)
            currentDoc.Save("test.kml")
        Catch ex As Exception
            Console.WriteLine(ex)
        End Try
    End Sub

代码后:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<gx:kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.opengis.net/kml/2.2">
  <gx:Document>
    <gx:name>test.kml</gx:name>
    <gx:Snippet>File Created Mon Jul 9 16:40:11 2012</gx:Snippet>
    <gx:Style id="multiTrack_n">
      <gx:IconStyle>
        <gx:Icon>
          <gx:href>http://maps.google.com/mapfiles/kml/shapes/track.png</gx:href>
        </gx:Icon>
      </gx:IconStyle>
      <gx:LineStyle>
        <gx:color>99ffac59</gx:color>
        <gx:width>6</gx:width>
      </gx:LineStyle>
    </gx:Style>
    <gx:Folder>
      <gx:name>test.kml</gx:name>
      <gx:Placemark>
        <gx:name>test</gx:name>
        <gx:styleUrl>#multiTrack_n</gx:styleUrl>
        <gx:Track>
          <!--GPS Tracking data Points-->
          <when xmlns="http://www.opengis.net/kml/2.2">2012-07-09T08:40:29Z</when>
          <gx:coord xmlns:gx="http://www.opengis.net/kml/2.2">0,0,0</gx:coord>
          <when xmlns="http://www.opengis.net/kml/2.2">2012-07-09T08:40:33Z</when>
          <gx:coord xmlns:gx="http://www.opengis.net/kml/2.2">0,0,0</gx:coord>
          <when xmlns="http://www.opengis.net/kml/2.2">2012-07-09T08:40:41Z</when>
          <gx:coord xmlns:gx="http://www.opengis.net/kml/2.2">0,0,0</gx:coord>
        </gx:Track>
      </gx:Placemark>
    </gx:Folder>
  </gx:Document>
</gx:kml>
4

1 回答 1

1

在意识到评论中所说的之后,我意识到如果我在 XML 中有默认 NS 作为http://www.opengis.net/kml/2.2,则假定每个元素都有它,所以当我重新保存时,它应用命名空间。

如此小而简单的东西......傻我......每天都学习一些东西

于 2012-07-10T14:53:13.290 回答