【发布时间】:2015-07-07 01:54:59
【问题描述】:
我需要能够从这个文件中提取单个节点到变量中以便进一步操作。我正在向控制台写信以查看正在提取哪些信息,但我正在努力提取名称或描述。
我可以成功打印整个文件。我尝试使用placemark.<name>.Value 和placemark.Element("name").Value 获取单个节点,其中第二个会引发NullReferenceException。关于如何在这种情况下提取名称和描述的任何想法?
Imports System.Xml
Imports System.Xml.Linq 'Visual Studio 2015 tells me this isn't needed
Imports System.Core 'Visual Studio 2015 tells me this isn't needed
Dim file As XDocument = XDocument.Load(filePath)
Dim placemarks As IEnumerable(Of XElement) = From test In file.Root.Elements()
For Each placemark As XElement In placemarks
Console.WriteLine(placemark) 'This works
Console.WriteLine(placemark.<name>.Value) 'This prints an empty line
Console.WriteLine(placemark.Element("description").Value) 'This throws a NullReferenceException
Next
这是结构
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Document>
<name>Untitled layer</name>
<Placemark>
<name>Name 1</name>
<description>Description 1</description>
<ExtendedData>
<Data name='Test data one'>
<value>Test data 1</value>
</Data>
</ExtendedData>
<Point>
<coordinates>34725567547</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Name 2</name>
<description>Description 2</description>
<ExtendedData>
<Data name='Test data two'>
<value>Test data 2</value>
</Data>
</ExtendedData>
<Point>
<coordinates>056795763767</coordinates>
</Point>
</Placemark>
【问题讨论】: