获取 XML 节点数据

发布于 2024-12-29 04:04:21 字数 4987 浏览 0 评论 0 原文

我是 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>

I am new to XML and am trying to get some of the information from this XML document:
http://pastebin.com/S7eUNmL2

Using this code:

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

The 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 技术交流群。

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

发布评论

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

评论(1

谈下烟灰 2025-01-05 04:04:21

加载 XmlDocument 时从节点中删除命名空间声明。

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

或者,您可以使用如下所示的命名空间管理器:

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

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

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

请参阅此 MSDN 上的文章。它在使用空间数据服务搜索 POI 部分提到“要将 XPath 查询与 Bing 地图 REST 服务结合使用,您必须创建 XmlNamespaceManager 并添加 REST 服务数据架构的 URL。”

当然,如果您在加载文档之前删除 xmlns 属性,您可以直接访问 XML 节点,我认为这更容易:)

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

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

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

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

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