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

发布于 2024-08-19 15:27:47 字数 1360 浏览 4 评论 0原文

此代码段来自此 答案

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 技术交流群。

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

发布评论

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

评论(3

卸妝后依然美 2024-08-26 15:27:47

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).匿名类型的元素将采用 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

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:

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

This gives you 3 different types of IEnumerable results.

  1. fotoElementsQuery will be of type IEnumerable(Of XElement)
  2. fotoAnonymousTypeQuery will be of type IEnumerable(Of <anonymous type>). The elements of the anonymous type will take on the names of the xml elements -- fotoname and fotoorder.
  3. fotoNamedTypeQuery will be of type IEnumeragle(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

岁月染过的梦 2024-08-26 15:27:47

为了充实其他一些答案:

在 VB.NET 中,您可以使用 XML Axis 运算符来简化方法语法。例如,.. 与 XElement("root").Elements("child") 相同。在这种情况下,子级必须位于根目录的正下方。如果您想要查找节点,无论它们存在于子节点中的何处,您可以使用 .Descendents 而不是 .Elements,或者使用带有三个点的 VB 语法,如下所示: ...。如果要访问属性,请使用 .@,如下所示:.@attributeName

从Murph的回复中,您可以用VB重写它,如下所示:

Fields = (From f In report.<field> _
          Select Name = f.@name, Type = f.@type).ToArray()

也可以使用Lambda语法编写如下:

Fields = report.<field> _
         .Select(Function(f) New With { _
             Name = f.@name, Type = f.@type).ToArray()

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:

Fields = (From f In report.<field> _
          Select Name = f.@name, Type = f.@type).ToArray()

This can also be written using the Lambda syntax as follows:

Fields = report.<field> _
         .Select(Function(f) New With { _
             Name = f.@name, Type = f.@type).ToArray()
你怎么这么可爱啊 2024-08-26 15:27:47

哈昨天读过这篇文章(如果我是对的 - 而且我可能不是 100% 掌握所有语法......)

Fields = report.Elements("field")
    .Select(Function(f) new { 
        Name = f.Attribute("name").Value,
        Type = f.Attribute("type").Value
    }).ToArray()

关键点 - 如果我的记忆正常 - 正在更改“f =>”到“函数(f)”

Hah read this yesterday (if I'm right - and I'm probably not 100% on all the syntax...)

Fields = report.Elements("field")
    .Select(Function(f) new { 
        Name = f.Attribute("name").Value,
        Type = f.Attribute("type").Value
    }).ToArray()

The key bit - if my memory is working right - is changing "f =>" to "Function(f)"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文