获取“
发布于 2024-08-25 13:24:37 字数 3477 浏览 7 评论 0 原文

我正在读取 Kml 文件,更改地标的名称,然后再次保存。

var KmlFile = XDocument.Load("C:\\Inetpub\\wwwroot\\GeotagService\\Kml\\Photographs.kml");

XNamespace KmlNamespace = "http://www.opengis.net/kml/2.2";

// find the Placemarks in the Photos folder
IEnumerable<XElement> Placemarks = KmlFile.Element(KmlNamespace + "kml").Element(KmlNamespace + "Document").Element(KmlNamespace + "Folder").Elements(KmlNamespace + "Placemark");

foreach (XElement p in Placemarks){
    p.Element(KmlNamespace + "name").Value = "testing";
}

KmlFile.Save("C:\\Inetpub\\wwwroot\\GeotagService\\Kml\\Photographs.kml");

然而,当我保存它时,每个元素都以 为前缀,如下所示:

<kml:Folder>
  <kml:name>Photos</kml:name>
  <kml:open>1</kml:open>
  <kml:Placemark>
    <kml:name>testing</kml:name>
    <kml:LookAt>
      <kml:longitude>-10.02717694938161</kml:longitude>
      <kml:latitude>53.48672543547379</kml:latitude>
      <kml:altitude>0</kml:altitude>
    </kml:LookAt>
    <kml:styleUrl>#msn_ylw-pushpin1</kml:styleUrl>
    <kml:Point>
       <kml:coordinates>-10.02867619360582,53.48651240326751,0</kml:coordinates>
    </kml:Point>
 </kml:Placemark>...

Tomarak 对 这个关于空白 xmlns 的问题给了我一个线索,即文档和元素的命名空间之间可能存在不一致,但我不明白我是怎么做的。有人知道吗?

编辑:原始文档(部分):

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <Document>
        <name>Connemara.net Photographs</name>
        <open>1</open>
        <Style id="sh_ylw-pushpin0">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                </Icon>
                <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
            </IconStyle>
        </Style>
        <Folder>
            <name>Photos</name>
            <open>1</open>
            <Placemark>
                <name>Id:579</name>
                <LookAt>
                    <longitude>-10.02717694938161</longitude>
                    <latitude>53.48672543547379</latitude>
                    <altitude>0</altitude>
                    <range>213.2931913230747</range>
                    <tilt>75.17546328115256</tilt>
                    <heading>69.89736514305363</heading>
                    <altitudeMode>relativeToGround</altitudeMode>
                    <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
                </LookAt>
                <styleUrl>#msn_ylw-pushpin1</styleUrl>
                <Point>
                    <coordinates>-10.02867619360582,53.48651240326751,0</coordinates>
                </Point>
            </Placemark>
...
</Folder>
</Document>
</kml>

I'm reading in a Kml file, changing the placemarks' names, and saving it again.

var KmlFile = XDocument.Load("C:\\Inetpub\\wwwroot\\GeotagService\\Kml\\Photographs.kml");

XNamespace KmlNamespace = "http://www.opengis.net/kml/2.2";

// find the Placemarks in the Photos folder
IEnumerable<XElement> Placemarks = KmlFile.Element(KmlNamespace + "kml").Element(KmlNamespace + "Document").Element(KmlNamespace + "Folder").Elements(KmlNamespace + "Placemark");

foreach (XElement p in Placemarks){
    p.Element(KmlNamespace + "name").Value = "testing";
}

KmlFile.Save("C:\\Inetpub\\wwwroot\\GeotagService\\Kml\\Photographs.kml");

When I save it however, every element is prefixed with <kml:, like this:

<kml:Folder>
  <kml:name>Photos</kml:name>
  <kml:open>1</kml:open>
  <kml:Placemark>
    <kml:name>testing</kml:name>
    <kml:LookAt>
      <kml:longitude>-10.02717694938161</kml:longitude>
      <kml:latitude>53.48672543547379</kml:latitude>
      <kml:altitude>0</kml:altitude>
    </kml:LookAt>
    <kml:styleUrl>#msn_ylw-pushpin1</kml:styleUrl>
    <kml:Point>
       <kml:coordinates>-10.02867619360582,53.48651240326751,0</kml:coordinates>
    </kml:Point>
 </kml:Placemark>...

Tomalak's comment on this question about blank xmlns gives me a clue that it might be inconsistencies between the namespaces of the document and the elements, but I can't see how I'm doing that. Anyone know?

EDIT: Original document (in part):

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
    <Document>
        <name>Connemara.net Photographs</name>
        <open>1</open>
        <Style id="sh_ylw-pushpin0">
            <IconStyle>
                <scale>1.3</scale>
                <Icon>
                    <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                </Icon>
                <hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
            </IconStyle>
        </Style>
        <Folder>
            <name>Photos</name>
            <open>1</open>
            <Placemark>
                <name>Id:579</name>
                <LookAt>
                    <longitude>-10.02717694938161</longitude>
                    <latitude>53.48672543547379</latitude>
                    <altitude>0</altitude>
                    <range>213.2931913230747</range>
                    <tilt>75.17546328115256</tilt>
                    <heading>69.89736514305363</heading>
                    <altitudeMode>relativeToGround</altitudeMode>
                    <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
                </LookAt>
                <styleUrl>#msn_ylw-pushpin1</styleUrl>
                <Point>
                    <coordinates>-10.02867619360582,53.48651240326751,0</coordinates>
                </Point>
            </Placemark>
...
</Folder>
</Document>
</kml>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2024-09-01 13:24:37

明白了 - 问题是原始文档是这样开始的:

 <kml xmlns="http://www.opengis.net/kml/2.2" 
      xmlns:gx="http://www.google.com/kml/ext/2.2" 
      xmlns:kml="http://www.opengis.net/kml/2.2" 
      xmlns:atom="http://www.w3.org/2005/Atom">

LINQ to XML 发现 KML 命名空间有一个特定的别名,因此使用它。如果删除该属性,它就可以正常工作:

var explicitNs = KmlFile.Root.Attribute(XNamespace.Xmlns + "kml");
if (explicitNs != null)
{
    explicitNs.Remove();
}

Got it - the problem is that the original document starts like this:

 <kml xmlns="http://www.opengis.net/kml/2.2" 
      xmlns:gx="http://www.google.com/kml/ext/2.2" 
      xmlns:kml="http://www.opengis.net/kml/2.2" 
      xmlns:atom="http://www.w3.org/2005/Atom">

LINQ to XML sees that there's a specific alias for the KML namespace, so uses that. If you remove that attribute, it works fine:

var explicitNs = KmlFile.Root.Attribute(XNamespace.Xmlns + "kml");
if (explicitNs != null)
{
    explicitNs.Remove();
}
一腔孤↑勇 2024-09-01 13:24:37

您需要使用命名空间管理器。以下是使用 XPath 选择必要标签然后更新其值的示例:

using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

class Program
{
    static void Main(string[] args)
    {
        var kml = XDocument.Load("test.kml");
        var ns = new XmlNamespaceManager(new NameTable());
        ns.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
        var names = kml.XPathSelectElements("//kml:Placemark/kml:name", ns);
        foreach (var name in names)
        {
            name.Value = "testing";
        }
        kml.Save("test.kml");
    }
}

You need to use a namespace manager. Here's an example using XPath to select the necessary tags and then update their value:

using System;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

class Program
{
    static void Main(string[] args)
    {
        var kml = XDocument.Load("test.kml");
        var ns = new XmlNamespaceManager(new NameTable());
        ns.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
        var names = kml.XPathSelectElements("//kml:Placemark/kml:name", ns);
        foreach (var name in names)
        {
            name.Value = "testing";
        }
        kml.Save("test.kml");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文