2010-10-17 8 views
1

J'essaie d'obtenir des éléments d'un fichier gpx (est un fichier xml). Ce n'est pas le schéma gpx standard. Il y a quelques changements de geocaching.com qui est un site web pour le géocaching.Extraire des éléments avec LinqToXML de GPX-File pour Geocaching

Mon échantillon XML-fichier lookslike ceci:

<?xml version="1.0" encoding="utf-8"?><gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0" creator="GPX Downloader" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0 http://www.groundspeak.com/cache/1/0/cache.xsd" xmlns="http://www.topografix.com/GPX/1/0"> 
    <bounds minlat="54.462917" minlon="11.242167" maxlat="54.462917" maxlon="11.242167" /> 
    <wpt lat="54.462917" lon="11.242167"> 
     <time>2008-09-07T00:00:00</time> 
     <name>GC1FYXH</name> 
     <desc>Kaffee &amp; Kuchen in Klaus' Dorf by barca2006, Multi-cache (1.5/1)</desc> 
     <url>http://www.geocaching.com/seek/cache_details.aspx?wp=GC1FYXH</url> 
     <urlname>Kaffee &amp; Kuchen in Klaus' Dorf</urlname> 
     <sym>Geocache</sym> 
     <type>Geocache|Multi-cache</type> 
     <groundspeak:cache id="988020" xmlns:groundspeak="http://www.groundspeak.com/cache/1/0" available="True" archived="False"> 
      <groundspeak:name>Kaffee &amp; Kuchen in Klaus' Dorf</groundspeak:name> 
      <groundspeak:placed_by>barca2006</groundspeak:placed_by> 
      <groundspeak:type>Multi-cache</groundspeak:type> 
      <groundspeak:container>Micro</groundspeak:container> 
      <groundspeak:difficulty>1.5</groundspeak:difficulty> 
      <groundspeak:terrain>1</groundspeak:terrain> 
      <groundspeak:short_description html="True"><![CDATA[]]></groundspeak:short_description> 
      <groundspeak:long_description html="True"><![CDATA[<div style="text-align: center;"><big><span style="font-family: Arial;"><img style="width: 254px; height: 350px;" alt="" src="http://img.geocaching.com/cache/fb049d7d-7da2-44f2-a5a8-a9b0c9ab6acb.jpg">&nbsp;&nbsp;&nbsp;</span></big> 
<img style="width: 290px; height: 350px;" alt="" src="http://img.geocaching.com/cache/1ab90dce-b07f-407b-8ad7-e2d7b60b31df.jpg"> 
<br> 
<big><span style="font-family: Arial;"><br> 
Dieser kleine Multi ist ein Dankeschön an Klaus (Hein Blöd),<br> 
der uns so viele bunte Dosen auf die Insel gelegt 
hat.<br></span></big><br> 
<div style="text-align: left;"> 
<div style="text-align: center;"><img style="width: 186px; height: 300px;" alt="" src="http://img.geocaching.com/cache/4dd24d09-be40-45b8-ac53-8d29eb2218bc.jpg">&nbsp;&nbsp; 
&nbsp;&nbsp; &nbsp; <img style="width: 245px; height: 300px;" alt="" src="http://img.geocaching.com/cache/f592905a-ffbd-44b7-8644-0fe357649a23.jpg"> 
</div> 
<br> 
<div style="text-align: center;">&nbsp; <big><span style="font-family: Arial;">Wie heißt die kleinste Schafsrasse der 
Welt?<br> 
_ _ _ _ _ _ _ _ -Schaf<br> 
Position des 8. Buchstabens im Alphabet = A<br> 
Position des 3. Buchstabens im Alphabet = B<br> 
<br> 
&nbsp;&nbsp;&nbsp; <img style="width: 166px; height: 200px;" alt="" src="http://img.geocaching.com/cache/cecb436e-b731-4363-9ca8-d472d438456e.jpg?rnd=0.9126512"> 
&nbsp;&nbsp;&nbsp;</span></big> <big><span style="font-family: Arial;"><img style="width: 313px; height: 200px;" alt="" src="http://img.geocaching.com/cache/a0edf8e2-15a6-40ff-a006-016087888c27.jpg?rnd=0.6537997"> 
</span></big><br> 
<big><span style="font-family: Arial;">Den Cache findet ihr 
hier:<br> 
N 54 27. (A x 38,6) E 011 14. (B x 103 
)<br></span></big><br> 
Wir wollten eigentlich am 05.09. nur Logbuchwechsel machen, haben 
die Dose aber gar nicht gefunden. Am richtigen Versteck war sie 
jedenfalls leider nicht. Wir haben eine neue Dose mit frischem 
Logbuch gelegt. Sollte jemand einen Logzettel mit Einträgen VOR dem 
5.9.2010 finden - bitte Dose + Logbuch entsorgen und Info an uns, 
vielen Dank!<br> 
<br> 
<big><span style="font-family: Arial;">Viel Spaß!</span></big><br> 
<br> 
&nbsp;&nbsp;</div> 
</div> 
<div style="text-align: left;"> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div> 
<br> 
<div style="text-align: right;"></div> 
</div> 
<br style="font-family: Arial;"> 
<br> 
]]> 
      </groundspeak:long_description> 
      <groundspeak:encoded_hints>Typisch Hein...</groundspeak:encoded_hints> 
      <groundspeak:logs> 
       <groundspeak:log id="129113034"> 
        <groundspeak:date>2010-09-29T00:00:00</groundspeak:date> 
        <groundspeak:type>Found it</groundspeak:type> 
        <groundspeak:finder id="559f0276-deff-438c-a4c3-e0501463d764"><![CDATA[cheesy67]]></groundspeak:finder> 
        <groundspeak:text><![CDATA[Spontan haben ako66 und ich uns zu einem Tagesausfluges von OWL nach Fehmarn entschlossen. Also die Fahrräder gesattelt und morgens um 4.00 Uhr los zum Dosen suchen. Auch diese lag auf dem zuvor geplantem Weg. 
DfdC sagt cheesy67 


]]></groundspeak:text> 
       </groundspeak:log> 
       <groundspeak:log id="129076512"> 
        <groundspeak:date>2010-09-29T00:00:00</groundspeak:date> 
        <groundspeak:type>Found it</groundspeak:type> 
        <groundspeak:finder id="2ae889c6-e31d-4f67-9c7b-a446c48b9410"><![CDATA[ako66]]></groundspeak:finder> 
        <groundspeak:text><![CDATA[Währen eines Tagesausfluges aus OWL nach Fehmarn zum Fahrrad fahren und Dosen suchen, sind cheesy67 und ich auch hier angehalten, um uns ins Logbuch einzutragen. Das Rätsel hatte ich bereits zu Hause gelöst, sodas es heute direkt zum Finale ging. 
DfdC sagt ako66 
]]></groundspeak:text> 
       </groundspeak:log> 
       <groundspeak:log id="128896060"> 
        <groundspeak:date>2010-09-28T00:00:00</groundspeak:date> 
        <groundspeak:type>Found it</groundspeak:type> 
        <groundspeak:finder id="a627af56-ca9a-4e18-b2b5-6a119f9b22e4"><![CDATA[Schnüffelnase Tom]]></groundspeak:finder> 
        <groundspeak:text><![CDATA[Aus dem Multi einen Mysterie gemacht und die Frage bereits zu Hause beantwortet, so konnte der Cache heute schnell gehoben werden. DFDC 
]]></groundspeak:text> 
       </groundspeak:log> 
       <groundspeak:log id="127939855"> 
        <groundspeak:date>2010-09-21T00:00:00</groundspeak:date> 
        <groundspeak:type>Found it</groundspeak:type> 
        <groundspeak:finder id="eab040c4-0b78-4d29-b3c9-d64b2f45ea5f"><![CDATA[murphy+tucker]]></groundspeak:finder> 
        <groundspeak:text><![CDATA[Heute nun Tag 3 unserer Fehmarn-Cacher-Ferien-Tour und bei wunderschönem Wetter konnten wir auch endlich wieder glücklich aufs Rad. Nach zähem Beginn, vor allen Dingen dadurch geprägt, dass bei diesem Wetter natürlich auch mehr Muggel unterwegs waren, ging es dann doch zügig voran und wir konnten einige Dosen glücklich als gefunden vermerken. So auch diese hier - vielen Dank für den Cache und die schöne Idee, sagen 
Murphy+Tucker 
]]></groundspeak:text> 
       </groundspeak:log> 
       <groundspeak:log id="127109027"> 
        <groundspeak:date>2010-09-16T00:00:00</groundspeak:date> 
        <groundspeak:type>Found it</groundspeak:type> 
        <groundspeak:finder id="d5062bf7-e9ba-4f92-b7c2-da2a9fe85b13"><![CDATA[tomtaucher]]></groundspeak:finder> 
        <groundspeak:text><![CDATA[Ein neuer Tag auf der Insel. Heute mit durchwachsenem Wetter, aber genug Sonnenschein und "Trockenzeiten", um eine schöne Tour über die Insel zu machen. Im eingespieltem Team mit rod_runner31+Lilli08 fanden wir auch diese Dose. Und weiter geht es. TFTC und Grüße von tomtaucher 
]]></groundspeak:text> 
       </groundspeak:log> 
      </groundspeak:logs> 
     </groundspeak:cache> 
    </wpt> 
</gpx> 

Mon LinqToXML-déclaration ressemble à ceci:

XDocument gpx = XDocument.Load(@"...GC1FYXH.gpx"); 

var caches = from e in gpx.Descendants("wpt") 
         select new 
          { 
           Name = e.Element("name").Value 
          }; 

Le fichier xml est chargé, mais je ne reçois aucun nom non. Quelqu'un peut-il aider?

Répondre

1

Ok, je l'ai. Il ne trouve rien parce qu'il y a plus d'un espace de noms dans le XML. C'est la même comme ici: Use Linq to Xml with Xml namespaces

Ainsi, la déclaration doit être droit:

var cache = from e in gpx.Descendants(ns + "wpt") 
      select new 
      { 
       Name = e.Element(ns + "name").Value 
      }; 
Questions connexes