获取 XML 节点数据
我是 XML 新手,正在尝试从此 XML 文档中获取一些信息: http://pastebin.com/S7eUNmL2
使用此代码:
Dim Document As New XmlDocument
Document.LoadXml(xml)
Dim DocumentElement As XmlElement = Document.DocumentElement
Dim ResourceSets As XmlNode = DocumentElement.ChildNodes.ItemOf(6)
Dim ResourceSet As XmlNode = ResourceSets.ChildNodes(0)
Dim Resource As XmlNode = ResourceSet.ChildNodes(1)
Dim LocationList As XmlNodeList = Resource.ChildNodes
Dim Location As XmlNode = LocationList.ItemOf(0)
Dim Name As String = Location.SelectSingleNode("Name").Value
但我得到一个未设置为实例的对象引用一个物体。最后一行代码出现异常。如果我快速观察 Location 值,它是正确的节点,但我不知道该怎么做......
XML:
<?xml version="1.0" encoding="utf-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<Copyright>
Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.
</Copyright>
<BrandLogoUri>
http://dev.virtualearth.net/Branding/logo_powered_by.png
</BrandLogoUri>
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<TraceId>e0aabdfcf9f746a39a4f3036b319720c|CPKM001259|02.00.83.500|CPKMSNVM001571, CPKMSNVM001585, CPKMSNVM001584, CPKMSNVM001587, CPKMSNVM001527, CPKMSNVM001502, CPKMSNVM001503</TraceId>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>5</EstimatedTotal>
<Resources>
<Location>
<Name>Perth, Australia</Name>
<Point>
<Latitude>-31.953020095825195</Latitude>
<Longitude>115.85723876953125</Longitude>
</Point>
<BoundingBox>
<SouthLatitude>-32.301349639892578</SouthLatitude>
<WestLongitude>115.20664978027344</WestLongitude>
<NorthLatitude>-31.608610153198242</NorthLatitude>
<EastLongitude>116.52772521972656</EastLongitude>
</BoundingBox>
<EntityType>PopulatedPlace</EntityType>
<Address>
<AdminDistrict>WA</AdminDistrict>
<CountryRegion>Australia</CountryRegion>
<FormattedAddress>Perth, Australia</FormattedAddress>
<Locality>Perth</Locality>
</Address>
<Confidence>High</Confidence>
<MatchCode>Good</MatchCode>
<GeocodePoint>
<Latitude>-31.953020095825195</Latitude>
<Longitude>115.85723876953125</Longitude>
<CalculationMethod>Rooftop</CalculationMethod>
<UsageType>Display</UsageType>
</GeocodePoint>
</Location>
<Location>
<Name>Perth, Perth and Kinross, United Kingdom</Name>
<Point>
<Latitude>56.396049499511719</Latitude>
<Longitude>-3.4324100017547607</Longitude>
</Point>
<BoundingBox>
<SouthLatitude>56.367079116519164</SouthLatitude>
<WestLongitude>-3.5021505233751609</WestLongitude>
<NorthLatitude>56.425019882504273</NorthLatitude>
<EastLongitude>-3.3626694801343606</EastLongitude>
</BoundingBox><EntityType>PopulatedPlace</EntityType>
<Address>
<AdminDistrict>Scotland</AdminDistrict>
<AdminDistrict2>Perth and Kinross</AdminDistrict2>
<CountryRegion>United Kingdom</CountryRegion>
<FormattedAddress>Perth, Perth and Kinross, United Kingdom</FormattedAddress>
<Locality>Perth</Locality>
</Address>
<Confidence>High</Confidence>
<MatchCode>Good</MatchCode>
<GeocodePoint>
<Latitude>56.396049499511719</Latitude>
<Longitude>-3.4324100017547607</Longitude>
<CalculationMethod>Rooftop</CalculationMethod>
<UsageType>Display</UsageType>
</GeocodePoint>
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加载 XmlDocument 时从节点中删除命名空间声明。
或者,您可以使用如下所示的命名空间管理器:
然后您可以在 XPath 查询中使用“rest:”声明,如下所示:
请参阅此 MSDN 上的文章。它在使用空间数据服务搜索 POI 部分提到“要将 XPath 查询与 Bing 地图 REST 服务结合使用,您必须创建 XmlNamespaceManager 并添加 REST 服务数据架构的 URL。”
当然,如果您在加载文档之前删除 xmlns 属性,您可以直接访问 XML 节点,我认为这更容易:)
Remove the namespace declaration from the node when you load the XmlDocument.
Alternatively you could use a namespace manager like this:
Then you can use the "rest:" declaration in your XPath queries like this:
See this article on MSDN. It mentions under the section Searching for POI using the Spatial Data Services that "To use XPath queries with the Bing Maps REST Services, you must create an XmlNamespaceManager and add the URL for the REST Services data schema."
Course if you just remove the xmlns attribute before loading the doc you can just access the XML nodes directly, which is easier in my opinion :)