VB.NET:在 Linq 中填充列表
假设我有以下 XML 文件:
<Movies>
<Movie ownerName="Ryan">
<Title>The Lord of the Rings Trilogy</Title>
<Title>Saving Private Ryan</Title>
<Title>etc</Title>
</Movie>
<Movie ownerName="Rynina">
<Title>Foo</Title>
<Title>Bar</Title>
</Movie>
</Movies>
我需要使用 Linq to Xml 来检索 MovieCollection 类型的类。 MovieCollection 有两个属性,OwnerName (String) 和 Movies (List(Of String))。
一般来说,我会这样做:
From entry in movies...<Movie>_
Select New MovieCollection With { _
.OwnerName = [email protected], _
.MovieCollection = entry.<Title>.Value}
但是在这种情况下,这显然行不通。无论如何,是否可以使用 Linq 使用该所有者发生的所有电影来填充 MovieCollection 列表?
Assume I have the following XML file:
<Movies>
<Movie ownerName="Ryan">
<Title>The Lord of the Rings Trilogy</Title>
<Title>Saving Private Ryan</Title>
<Title>etc</Title>
</Movie>
<Movie ownerName="Rynina">
<Title>Foo</Title>
<Title>Bar</Title>
</Movie>
</Movies>
What I'm after is using Linq to Xml to retrieve classes of type MovieCollection. MovieCollection has two properties, OwnerName (String) and Movies (List(Of String)).
Generally, I'd do something like:
From entry in movies...<Movie>_
Select New MovieCollection With { _
.OwnerName = [email protected], _
.MovieCollection = entry.<Title>.Value}
However in this case this obviously wont work. Is there anyway to fill the MovieCollection list with all the movies that occur for that owner using Linq?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实应该将标签命名为
Movie
MovieCollection
和Title
Movie
。使用以下命令解析您的 XML:
或
并使用:
You should really name the tags
Movie
MovieCollection
andTitle
Movie
.Parse your XML with:
or
and use this:
尝试使用序列化
http://support.microsoft.com/kb/316730
Try to use the serialization
http://support.microsoft.com/kb/316730