在 VB.NET 中查询 LINQ to XML 源
我有以下 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&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>
...
这是我尝试使用的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
或
Try
or
这是使用 xml 文字语法的版本:
Here's the version using xml literal syntax: