Linq to XML 如何在 vb.net 中执行此操作
此代码段来自此 答案
var reports = from report in xml.Descendants("report")
where report.Element("name").Value.Contains("Adjustment Report")
select new {
Name = report.Element("name").Value,
Extension = report.Element("extension").Value,
FileType = report.Element("filetype").Value,
Fields = report.Elements("field")
.Select(f => new {
Name = f.Attribute("name").Value,
Type = f.Attribute("type").Value
}).ToArray()
};
对于我的一生,我无法弄清楚 vb.net 中这部分的语法:
Fields = report.Elements("field")
.Select(**f =>** new {
Name = f.Attribute("name").Value,
Type = f.Attribute("type").Value
}).ToArray()
我想要完成的事情 - 我的 xml 看起来像这样:
<items>
<item>
<id>data</id>
<foto>
<fotoname>img1.jpg</fotoname>
<fotoorder>1</fotoorder>
</foto>
<foto>
<fotoname>img2.jpg</fotoname>
<fotoorder>2</fotoorder>
</foto>
</item>
</items>
我需要我的对象有一个列表(或任何种类)的照片元素。
This snippet is from this answer
var reports = from report in xml.Descendants("report")
where report.Element("name").Value.Contains("Adjustment Report")
select new {
Name = report.Element("name").Value,
Extension = report.Element("extension").Value,
FileType = report.Element("filetype").Value,
Fields = report.Elements("field")
.Select(f => new {
Name = f.Attribute("name").Value,
Type = f.Attribute("type").Value
}).ToArray()
};
For the life of me I cannot figure out the syntax for this part in vb.net:
Fields = report.Elements("field")
.Select(**f =>** new {
Name = f.Attribute("name").Value,
Type = f.Attribute("type").Value
}).ToArray()
What I'm trying to accomplish - my xml looks like this:
<items>
<item>
<id>data</id>
<foto>
<fotoname>img1.jpg</fotoname>
<fotoorder>1</fotoorder>
</foto>
<foto>
<fotoname>img2.jpg</fotoname>
<fotoorder>2</fotoorder>
</foto>
</item>
</items>
I need my object to have a List (or collection of any kind) of foto elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ to XML 是 VB.NET 提供与 C# 完全不同的语法的领域之一。您可以使用相同的方法链接,但我更喜欢如下所示的 VB.NET LINQ 语法:
这为您提供了 3 种不同类型的 IEnumerable 结果。
fotoElementsQuery
的类型为IEnumerable(Of XElement)
fotoAnonymousTypeQuery
的类型为IEnumerable(Of).匿名类型的元素将采用 xml 元素的名称 -
fotoname
和fotoorder
。fotoNamedTypeQuery
的类型为IEnumeragle(Of Foto)
在上面的代码中,LINQ 查询尚未实际执行。为了获取列表(并执行查询),请调用
.ToList()
或.ToArray()
扩展方法。更新:了解 VB.NET 中 LINQ(和 LINQ to XML)优点的最佳方法是观看 Beth Massi 的 How Do I 视频系列。 http://msdn.microsoft.com/en-us/vbasic/ bb466226.aspx#linq
LINQ to XML is one of the areas where VB.NET offers a completely different syntax than C#. You can use the same method chaining, but I prefer the VB.NET LINQ syntax that looks like this:
This gives you 3 different types of IEnumerable results.
fotoElementsQuery
will be of typeIEnumerable(Of XElement)
fotoAnonymousTypeQuery
will be of typeIEnumerable(Of <anonymous type>)
. The elements of the anonymous type will take on the names of the xml elements --fotoname
andfotoorder
.fotoNamedTypeQuery
will be of typeIEnumeragle(Of Foto)
The LINQ queries haven't actually executed yet in the above code. In order to get a List (and to execute the query) call either the
.ToList()
or.ToArray()
extension method.Update: The best way to learn about the goodness that is LINQ (and LINQ to XML) in VB.NET is by watching the How Do I Video Series by Beth Massi. http://msdn.microsoft.com/en-us/vbasic/bb466226.aspx#linq
为了充实其他一些答案:
在 VB.NET 中,您可以使用 XML Axis 运算符来简化方法语法。例如,
..
与 XElement("root").Elements("child") 相同。在这种情况下,子级必须位于根目录的正下方。如果您想要查找节点,无论它们存在于子节点中的何处,您可以使用.Descendents
而不是.Elements
,或者使用带有三个点的 VB 语法,如下所示:...
。如果要访问属性,请使用.@
,如下所示:.@attributeName
。从Murph的回复中,您可以用VB重写它,如下所示:
也可以使用Lambda语法编写如下:
To flesh out some of the other answers:
In VB.NET, you can use XML Axis operators to simplify the method syntax. For example
<root>..<child>
is the same as XElement("root").Elements("child"). In this case, the child has to be directly under the root. If you want to find nodes regardless of where they exist in the child nodes you can either use.Descendents
instead of.Elements
, or the VB syntax with three dots as follows:<root>...<descendentNodeName>
. If you want to access an attribute, use.@
as follows:<root>.@attributeName
.From Murph's response, you can re-write it in VB as follows:
This can also be written using the Lambda syntax as follows:
哈昨天读过这篇文章(如果我是对的 - 而且我可能不是 100% 掌握所有语法......)
关键点 - 如果我的记忆正常 - 正在更改“f =>”到“函数(f)”
Hah read this yesterday (if I'm right - and I'm probably not 100% on all the syntax...)
The key bit - if my memory is working right - is changing "f =>" to "Function(f)"