vb.net - Linq to XML 如何在 vb.net 中执行此操作

标签 vb.net linq-to-xml

此片段来自 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()
    };

对于我的一生,我无法在 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>

我需要我的对象有一个照片元素列表(或任何类型的集合)。

最佳答案

LINQ to XML 是 VB.NET 提供与 C# 完全不同的语法的领域之一。您可以使用相同的方法链接,但我更喜欢如下所示的 VB.NET LINQ 语法:

Sub Main()
    Dim myXml = <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>

    Dim fotoElementsQuery = From f In myXml...<foto> _
                            Select f

    Dim fotoAnonymousTypeQuery = From f In myXml...<foto> _
                                 Select f.<fotoname>.Value, f.<fotoorder>.Value

    Dim fotoNamedTypeQuery = From f In myXml...<foto> _
                             Select New Foto With {.Name = f.<fotoname>.Value, .Order = f.<fotoorder>.Value}

End Sub

Public Class Foto

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _order As Integer
    Public Property Order() As Integer
        Get
            Return _order
        End Get
        Set(ByVal value As Integer)
            _order = value
        End Set
    End Property

End Class

这为您提供了 3 种不同类型的 IEnumerable 结果。

  1. fotoElementsQuery将是 IEnumerable(Of XElement) 类型
  2. fotoAnonymousTypeQuery将是 IEnumerable(Of <anonymous type>) 类型.匿名类型的元素将采用 xml 元素的名称 -- fotonamefotoorder .
  3. 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

关于vb.net - Linq to XML 如何在 vb.net 中执行此操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2154432/

相关文章:

.net - 如何在 VB.NET 中的每一行添加注释?

asp.net - System.NullReferenceException : Object reference not set to an instance of an object. in vb.net

c++ - 在 C++ 中准确表示 Visual Basic Decimal 变量的替代方法

c# - 如何从 XDocument 对象中获取子元素列表?

c# - 在 C# 中从 XML 添加/删除元素

.net - 无法在 Visual Studio Express VB.net 中构建

vb.net - vb中的微积分引擎?

c# - SharePoint Web 服务的 LINQ to XML 问题

c# - xdocument save 保留标签内的空白

c# - 将 XML 元素读取到 ListView - 我如何将两个变量传递给方法