在 VB.NET 中查询 LINQ to XML 源

发布于 2024-10-21 17:16:29 字数 2224 浏览 1 评论 0原文

我有以下 XML,通过 XDocument.Load(uri)XElement.Load(uri) 加载。我无法通过 LINQ 获取 元素的集合。

这是我尝试查询的 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

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&Format=XML" />
      <link rel="first" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&Format=XML" />
      <link rel="next" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&Format=XML&page=2" />
      <link rel="last" href="http://web.aisle7.net/api/1.0/us/assets/~all?apikey=1234567890&Format=XML&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&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&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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

忘羡 2024-10-28 17:16:29

尝试

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

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
此刻的回忆 2024-10-28 17:16:29

这是使用 xml 文字语法的版本:

Dim xml = XElement.Load(uri)
Dim q = From a In xml.<assets>...<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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文