2012-01-25 167 views
0

我是新來的XML,我試圖得到一些從這個XML文檔中的信息: http://pastebin.com/S7eUNmL2獲取XML節點數據

使用此代碼:

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 

但我得到一個對象引用未設置爲一個對象的實例。在最後一行代碼中出現異常。如果我快速監視位置值是正確的節點,但我在做什麼損失......

的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> 
+0

你確定你NIT得到一個異常容易過嗎? –

+0

我沒有得到代碼的任何異常,我嘗試使用XDocument,但我得到關於WhiteSpace字符的異常。 –

回答

1

從節點中刪除該命名空間聲明時加載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查詢這樣的:

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

見本article在MSDN上。它在部分中提到使用空間數據服務搜索POI,即「要將Bingath Maps REST服務使用XPath查詢,必須創建一個XmlNamespaceManager併爲REST Services數據模式添加URL。」

當然,如果你只是刪除xmlns屬性加載文檔你可以直接訪問XML節點,這在我看來:)