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

发布于 2024-07-07 04:38:33 字数 878 浏览 7 评论 0原文

我正在尝试从 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

我想将整个 ... 块捕获为字符串。 我尝试了类似的方法(其中...在上面):

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

但 XElement 对象没有 InnerText 属性,或者据我所知没有任何等效属性。 如何获取定义 XElement 的原始 XML?

I'm trying to extract the polygons from placemarks 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'd like to capture the entire <polygon>...</polygon> block as a string. I tried something like this (where the ... is above):

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

but the XElement object doesn't have an InnerText property, or any equivalent as far as I can tell. How do I grab the raw XML that defines an XElement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生戏 2024-07-14 04:38:33

我缺少的是 Placemark. 是 XElement 的集合,而不是单个 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

正如 tbrownell 建议的那样,XElement.ToString 相当于 InnerText。

What I was missing was that Placemark.<g:Polygon> is a collection of XElements, not a single XElement. This 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.

天荒地未老 2024-07-14 04:38:33

你有没有尝试过:

Placemark.ToString()

Have you tried:

Placemark.ToString()
默嘫て 2024-07-14 04:38:33

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

(string)Placemark.<g:name>

抱歉,我不确定 VB 语法,,,自从我用 VB 编写代码以来已经有一段时间了。

I missed the Enumeration also. When using .Value it is possible to receive a null exception. Try the equivelent of this instead:

(string)Placemark.<g:name>

Sorry not sure of the VB syntax,,,it has been a while since I have coded in VB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文