Question

J'essaie d'extraire les polygones des repères d'un fichier KML. Jusqu'ici tout va bien:

Imports <xmlns:g='http://earth.google.com/kml/2.0'>
Imports System.Xml.Linq

Partial Class Test_ImportPolygons
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Kml As XDocument = XDocument.Load(Server.MapPath("../kmlimport/ga.kml"))
        For Each Placemark As XElement In Kml.<g:Document>.<g:Folder>.<g:Placemark>
            Dim Name As String = Placemark.<g:name>.Value
            ...
        Next
    End Sub

End Class

Je voudrais capturer l'intégralité du bloc <polygon>...</polygon> en tant que chaîne. J'ai essayé quelque chose comme ça (où le ... est en haut):

        Dim Polygon as String = Placemark.<g:Polygon>.InnerText

mais l'objet XElement n'a pas de propriété InnerText, ni d'équivalent pour autant que je sache. Comment récupérer le XML brut qui définit un XElement?

Était-ce utile?

La solution

Ce qui me manquait était que Placemark.<g:Polygon> était une collection de XElements, pas un seul XElement. Cela fonctionne:

    For Each Placemark As XElement In Kml.<g:Document>.<g:Folder>.<g:Placemark>
        Dim Name As String = Placemark.<g:name>.Value
        Dim PolygonsXml As String = ""
        For Each Polygon As XElement In Placemark.<g:Polygon>
            PolygonsXml &= Polygon.ToString
        Next
    Next

XElement.ToString est l'équivalent d'InnerText, comme suggéré par tbrownell.

Autres conseils

Avez-vous essayé:

Placemark.ToString()

J'ai aussi manqué l'énumération. Lorsque vous utilisez .Value, il est possible de recevoir une exception NULL. Essayez plutôt l'équivalent de ceci:

(string)Placemark.<g:name>

Désolé, je ne suis pas sûr de la syntaxe VB ", cela fait longtemps que je n'ai pas codé en VB.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top