如何在 Linq to XML 中获取 XElement 的 .InnerText 值?
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我缺少的是
Placemark.
是 XElement 的集合,而不是单个 XElement。 这是可行的:正如 tbrownell 建议的那样,XElement.ToString 相当于 InnerText。
What I was missing was that
Placemark.<g:Polygon>
is a collection of XElements, not a single XElement. This works:XElement.ToString is the equivalent of InnerText, as tbrownell suggested.
你有没有尝试过:
Have you tried:
我也错过了枚举。 使用 .Value 时可能会收到 null 异常。 尝试等效的方法:
抱歉,我不确定 VB 语法,,,自从我用 VB 编写代码以来已经有一段时间了。
I missed the Enumeration also. When using .Value it is possible to receive a null exception. Try the equivelent of this instead:
Sorry not sure of the VB syntax,,,it has been a while since I have coded in VB.