Linq to XML how to do it in vb.net

This snippet 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()
    };

In life, I cannot understand the syntax of 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 do is 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 type) of photo elements.

+3
source share
3 answers

LINQ to XML is one area where VB.NET offers a completely different syntax than C #. You can use the same method chain, but I prefer the VB.NET LINQ syntax, which 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.

  • fotoElementsQuery will be of type IEnumerable(Of XElement)
  • fotoAnonymousTypeQuery IEnumerable(Of <anonymous type>). xml - fotoname fotoorder.
  • fotoNamedTypeQuery IEnumeragle(Of Foto)

LINQ . ( ), .ToList() .ToArray().

: , LINQ ( LINQ to XML) VB.NET, - " " . http://msdn.microsoft.com/en-us/vbasic/bb466226.aspx#linq

+5

:

VB.NET XML Axis . , <root>..<child> XElement ( "root" ). ( "child" ). . , , , .Descendents .Elements VB : <root>...<descendentNodeName>. , .@ : <root>.@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()
+5

( - , , 100% ...)

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

- - "f = > " " (f)"

0

Source: https://habr.com/ru/post/1730377/


All Articles