-
21-09-2019 - |
Question
Je lis dans un fichier Kml, en changeant les noms des repères, et l'enregistrer à nouveau.
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");
Quand je l'enregistre cependant, tous est préfixé avec <kml:
, comme ceci:
<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>...
Commentaire de Tomalak sur cette question sur xmlns blanc donne moi un indice qu'il pourrait y avoir des incohérences entre les namespaces du document et les éléments, mais je ne vois pas comment je fais ça. Quelqu'un sait?
EDIT : Document original (en partie):
<?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>
La solution
Got it - le problème est que le document original commence comme ceci:
<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 voit qu'il ya un alias spécifique pour l'espace de noms KML, donc l'utilise. Si vous supprimez cet attribut, il fonctionne très bien:
var explicitNs = KmlFile.Root.Attribute(XNamespace.Xmlns + "kml");
if (explicitNs != null)
{
explicitNs.Remove();
}
Autres conseils
Vous devez utiliser un espace de noms gestionnaire . Voici un exemple en utilisant XPath pour sélectionner les balises nécessaires et mettre à jour leur valeur:
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");
}
}