开发者

Querying a LINQ to XML feed in VB.NET

开发者 https://www.devze.com 2023-02-16 08:09 出处:网络
I have the following XML which I load via XDocument.Load(uri) or XElement.Load(uri). I am having trouble getting a collection of <asset> elements via LINQ.

I have the following XML which I load via XDocument.Load(uri) or XElement.Load(uri). I am having trouble getting a collection of <asset> elements via LINQ.

Here is a snippet of the XML I'm trying to query:

<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?a开发者_C百科pikey=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>
    ...

And here is the code I'm trying to use:

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


Try

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

or

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


Here's the version using xml literal syntax:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号