如何在Linq中获取XElement的.InnerText值到XML?

时间:2008-09-30 14:54:57

标签: vb.net linq linq-to-xml kml

我正在尝试从KML文件中的地标中提取多边形。到目前为止一切顺利:

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

我想将整个<polygon>...</polygon>块捕获为字符串。我试过这样的事情(上面的......):

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

但是XElement对象没有InnerText属性,或者就我所知的任何等价物而言。如何获取定义XElement的原始XML?

3 个答案:

答案 0 :(得分:1)

你试过了吗?

Placemark.ToString()

答案 1 :(得分:1)

我缺少的是Placemark.<g:Polygon>是XElements的集合,而不是单个XElement。这有效:

    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相当于InnerText,正如tbrownell建议的那样。

答案 2 :(得分:0)

我也错过了枚举。使用.Value时,可能会收到null异常。试试相同的方法:

(string)Placemark.<g:name>

抱歉不确定VB语法,,,因为我用VB编码已经有一段时间了。