1

I have an XML like this:

<?xml version="1.0" encoding="Windows-1252"?>
<!--XML Songs Database.-->
<Songs>
    <Song><Name>My Song 1.mp3</Name><Year>2007</Year><Genre>Dance</Genre><Bitrate>320</Bitrate><Length>04:55</Length><Size>4,80</Size></Song>
    <Song><Name>My Song 2.mp3</Name><Year>2009</Year><Genre>Electro</Genre><Bitrate>192</Bitrate><Length>06:44</Length><Size>8,43</Size></Song>
    <Song><Name>My Song 3.mp3</Name><Year>2008</Year><Genre>UK Hardcore</Genre><Bitrate>128</Bitrate><Length>05:12</Length><Size>4,20</Size></Song>
</Songs>

I would like to store the elements into an Array or something similar to easy read them.

I think that a Lis of Tuples would be a nice collection "container", if not then I can hear suggestions.

I did the conversion but not using LINQ, I want to simplify the code below creating the List of Tuples using LINQ instead of using a FOR + Select case, so I need help to rewrite/improve this code:

Dim Name As String = String.Empty
Dim Year As String = String.Empty
Dim Genre As String = String.Empty
Dim Bitrate As String = String.Empty
Dim Length As String = String.Empty
Dim Size As String = String.Empty

Dim SongsList As New List(Of Tuple(Of String, String, String, String, String, String))
Dim Elements As IEnumerable(Of XElement) = XDocument.Load(xmlfile).Descendants()

For Each Element As XElement In Elements

    Select Case Element.Name

        Case "Name"
            Name = Element.Value
        Case "Year"
            Year = Element.Value
        Case "Genre"
            Genre = Element.Value
        Case "Bitrate"
            Bitrate = Element.Value
        Case "Length"
            Length = Element.Value
        Case "Size"
            Size = Element.Value
            SongsList.Add(Tuple.Create(Name, Year, Genre, Bitrate, Length, Size))

    End Select

Next

And to read the songs I do this:

For Each song As Tuple(Of String, String, String, String, String, String) In SongsList

    MsgBox(String.Format("Name:{1}{0}Year:{2}{0}Genre:{3}{0}Bitrate:{4}{0}Length:{5}{0}Size:{6}", _
                         Environment.NewLine, _
                         song.Item1, song.Item2, song.Item3, song.Item4, song.Item5, song.Item6))

    ' Output:
    '
    ' Name:My Song 1.mp3
    ' Year:2007
    ' Genre:Dance
    ' Bitrate:320
    ' Length:04:55
    ' Size:4,80

Next
4

1 回答 1

1

这很容易

Dim xml = <?xml version="1.0" encoding="Windows-1252"?>
            <!--XML Songs Database.-->
            <Songs>
                <Song><Name>My Song 1.mp3</Name><Year>2007</Year><Genre>Dance</Genre><Bitrate>320</Bitrate><Length>04:55</Length><Size>4,80</Size></Song>
                <Song><Name>My Song 2.mp3</Name><Year>2009</Year><Genre>Electro</Genre><Bitrate>192</Bitrate><Length>06:44</Length><Size>8,43</Size></Song>
                <Song><Name>My Song 3.mp3</Name><Year>2008</Year><Genre>UK Hardcore</Genre><Bitrate>128</Bitrate><Length>05:12</Length><Size>4,20</Size></Song>
            </Songs>

Dim result = from song in xml.<Songs>.<Song>
             select Tuple.Create(song.<Name>.Value, 
                                 song.<Year>.Value, 
                                 song.<Genre>.Value,
                                 song.<Bitrate>.Value,
                                 song.<Length>.Value,
                                 song.<Size>.Value)

Tuple但是你也可以使用匿名类型来代替怪物。这很容易

Dim result = from song in xml.<Songs>.<Song>
             select new with {song.<Name>.Value, 
                              song.<Year>.Value, 
                              song.<Genre>.Value,
                              song.<Bitrate>.Value,
                              song.<Length>.Value,
                              song.<Size>.Value}

因此您可以通过有意义的名称访问Name,等,例如YearGenre

For Each song In result
    Console.WriteLine(String.Format("Name:{1}{0}Year:{2}{0}Genre:{3}{0}Bitrate:{4}{0}Length:{5}{0}Size:{6}", _
                                    Environment.NewLine, _
                                    song.Name, song.Year, song.Genre, song.Bitrate, song.Length, song.Size))
Next 

而不是无意义Item1Item2,Item3

于 2013-10-24T13:25:11.977 回答