获取XML节点数据 [英] Get XML Node Data

查看:65
本文介绍了获取XML节点数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是XML的新手,正在尝试从此XML文档中获取一些信息: http://pastebin.com/S7eUNmL2

I am new to XML and am trying to get some of the information from this XML document: 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

但是我得到一个没有设置为对象实例的对象引用.最后一行代码上的异常.如果我快速查看位置"值,则它是正确的节点,但是我不知道该怎么做...

But I get an Object reference not set to an instance of an object. exception on the last line of code. If I quickwatch the Location value it is the correct node, but I'm at a loss of what to do...

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>

推荐答案

在加载XmlDocument时从节点删除名称空间声明.

Remove the namespace declaration from the node when you load the XmlDocument.

Document.LoadXml(xml.Replace("xmlns=""http://schemas.microsoft.com/search/local/ws/rest/v1""",""))

或者,您可以使用这样的名称空间管理器:

Alternatively you could use a namespace manager like this:

Document.LoadXml(xml)
Dim nsmgr As New XmlNamespaceManager(Document.NameTable)
nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/v1")

然后您可以在XPath查询中使用"rest:"声明,如下所示:

Then you can use the "rest:" declaration in your XPath queries like this:

Dim Name As String = Location.SelectSingleNode("rest:Name", nsmgr).Value

请参见MSDN上的文章.它在使用空间数据服务搜索POI 部分中提到:要将XPath查询与Bing Maps REST服务一起使用,必须创建XmlNamespaceManager并添加REST Services数据模式的URL."

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."

当然,如果您只是在加载文档之前删除xmlns属性,则可以直接访问XML节点,这在我看来很容易:)

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 :)

这篇关于获取XML节点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆