2011-03-09 89 views
1

我有以下XML,我通過XDocument.Load(uri)XElement.Load(uri)加載。我無法通過LINQ獲取<asset>元素的集合。在VB.NET中查詢LINQ to XML提要

這裏是XML我想要查詢的一個片段:

<assetCollection xmlns="tag:aisle7.net,2009:/api/1.0"> 
     <title>All Assets</title> 
     <description>Collection containing all assets in the system</description> 
     <resourcePath>/us/assets/~all</resourcePath> 
     <link rel="self" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&amp;Format=XML" /> 
     <link rel="first" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&amp;Format=XML" /> 
     <link rel="next" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&amp;Format=XML&amp;page=2" /> 
     <link rel="last" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&amp;Format=XML&amp;page=66" /> 
     <updated>2011-03-01T19:01:49.667Z</updated> 
     <assets> 
     <asset> 
      <title>Homeopathy</title> 
      <resourcePath>/us/assets/toc/homeopathy</resourcePath> 
      <link rel="alternate" href="http://web.aisle7.net/api/1.0/us/assets/toc/homeopathy?apikey=1234567890&amp;Format=XML" /> 
      <updated>2011-03-01T19:01:49.667Z</updated> 
     </asset> 
     <asset> 
      <title>What Is Homeopathy?</title> 
      <resourcePath>/us/assets/generic/what-is-homeopathy_13615_1</resourcePath> 
      <link rel="alternate" href="http://web.aisle7.net/api/1.0/us/assets/generic/what-is-homeopathy_13615_1?apikey=1234567890&amp;Format=XML" /> 
      <updated>2011-03-01T19:00:17.680Z</updated> 
     </asset> 
    ... 

,這裏是我想要使用的代碼:

Dim uri As String = HttpUtility.UrlDecode(ConfigurationManager.AppSettings("Aisle7_Index_Url")) 

Dim assets = (From a In XElement.Load(uri) 
           .Element("assets") 
           .Elements("asset") 
      Select a) 

For Each asset In assets 
    Console.WriteLine(asset) 
Next 

回答

2

嘗試

Dim assets = From a In XElement.Load(uri).Descendants("asset") Select a 

Dim assets = From a In XDocument.Load(uri).Root.Element("assets").Elements("asset") Select a 
+0

沒有骰子。調試'資產'顯示消息「類型'異常'System.Linq.SystemCore_EnumerableDebugViewEmptyException'被拋出。」在檢查員。有任何想法嗎? – 2011-03-09 20:38:37

+0

你剛剛發佈的第二個代碼段拋出一個NullReferenceException :( – 2011-03-09 20:44:07

+0

@Mark顯然它不喜歡你的'xmlns =「標籤:aisle7.net,2009:/api/1.0」'。如果我刪除它,兩個代碼段都可以正常工作。 ? – 2011-03-09 20:51:36

0

這裏的版中使用XML語法:

Dim xml = XElement.Load(uri) 
Dim q = From a In xml.<assets>...<asset> 
     Select a