How can I get the XElement.InnerText value in Linq for XML?

I am trying to extract polygons from labels in a KML file. So far so good:

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

I would like to grab the whole block <polygon>...</polygon>as a string. I tried something like this (where ... above):

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

but the XElement object does not have an InnerText property or any equivalent as far as I can tell. How can I capture the raw XML that defines the XElement?

+3
source share
3 answers

What I was missing was what Placemark.<g:Polygon>was a set of XElements, not just one XElement. It works:

    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 is the equivalent of InnerText, as tbrownell suggested.

+1
source

You tried:

Placemark.ToString()
+1
source

. .Value . equivelent:

(string)Placemark.<g:name>

Sorry I'm not sure about the syntax of VB, some time has passed since I am encoded in VB.

0
source

Source: https://habr.com/ru/post/1697723/


All Articles