< XML版本=1.0编码=视窗1252&GT?;
< - !XML歌曲数据库.-->
<诗经>
&LT;宋&GT;&LT;名称&gt;我的歌1.mp3</Name><Year>2007</Year><Genre>Dance</Genre><Bitrate>320</Bitrate><Length>04:55</Length><Size>4,80</Size></Song>
&LT;宋&GT;&LT;名称&gt;我的歌2.mp3</Name><Year>2009</Year><Genre>Electro</Genre><Bitrate>192</Bitrate><Length>06:44</Length><Size>8,43</Size></Song>
&LT;宋&GT;&LT;名称&gt;我的歌3.MP3&LT; /名称&gt;&LT;年&GT; 2008&LT; /年&GT;&LT;类型&GT;英国Hardcore</Genre><Bitrate>128</Bitrate><Length>05:12</Length><Size>4,20</Size></Song>
&LT; /歌曲&GT;
我想存储的元素放进一个数组或类似的东西容易阅读。
我认为,一个元组的李氏将是一个很好的集合的容器,如果没有的话我能听到的建议。
我做了转换,但不使用LINQ,我想简化下面创建的元组使用LINQ的列表,而不是使用FOR +选择情况下,code,所以我需要帮助重写/改善这种code:
昏暗的名称作为字符串=的String.Empty
昏暗年作为字符串=的String.Empty
昏暗类型为String =的String.Empty
昏暗比特率作为字符串=的String.Empty
昏暗的长度字符串=的String.Empty
昏暗的大小作为字符串=的String.Empty
昏暗SongsList作为新的列表(元组(字符串,字符串,字符串,字符串,字符串,字符串))
昏暗元素为IEnumerable(中的XElement)= XDocument.Load(XMLFILE).Descendants()
对于每个元素的XElement中的元素
选择案例Element.Name
案姓名
NAME = Element.Value
案年
年= Element.Value
案流派
类型= Element.Value
案比特率
比特率= Element.Value
案长度
长度= Element.Value
案大小
大小= Element.Value
SongsList.Add(Tuple.Create(名称,年份,流派,比特率,长度,尺寸))
最终选择
下一个
和阅读我这样做的歌曲:
为每首歌曲作为元组(字符串,字符串,字符串,字符串,字符串,字符串)在SongsList
MSGBOX(的String.Format(名称:{1} {0}年份:{2} {0}类型:{3} {0}比特率:{4} {0}长度:{5} {0}大小:{ 6},_
Environment.NewLine,_
song.Item1,song.Item2,song.Item3,song.Item4,song.Item5,song.Item6))
输出:
姓名:我的歌1.MP3
年份:2007
流派:Dance
比特率:320
'长度:04:55
'大小:4,80
下一个
解决方案
这是那么容易,因为
暗淡XML =&LT; XML版本=1.0编码=视窗1252&GT?;
&LT; - !XML歌曲数据库.--&GT;
&LT;诗经&GT;
&LT;宋&GT;&LT;名称&gt;我的歌1.mp3</Name><Year>2007</Year><Genre>Dance</Genre><Bitrate>320</Bitrate><Length>04:55</Length><Size>4,80</Size></Song>
&LT;宋&GT;&LT;名称&gt;我的歌2.mp3</Name><Year>2009</Year><Genre>Electro</Genre><Bitrate>192</Bitrate><Length>06:44</Length><Size>8,43</Size></Song>
&LT;宋&GT;&LT;名称&gt;我的歌3.MP3&LT; /名称&gt;&LT;年&GT; 2008&LT; /年&GT;&LT;类型&GT;英国Hardcore</Genre><Bitrate>128</Bitrate><Length>05:12</Length><Size>4,20</Size></Song>
&LT; /歌曲&GT;
昏暗的结果=从歌曲在XML和LT;诗经&GT;&LT;宋&GT;
选择Tuple.Create(曲&LT;名称&gt; .value的,
歌曲与LT;年&GT; .value的,
歌曲与LT;类型&GT; .value的,
歌曲与LT;比特率&GT; .value的,
歌曲与LT;长度&GT; .value的,
歌曲与LT;大小&GT;。价值)
但是,而不是一个怪物元组
,你也可以使用匿名类型。这很容易为
暗淡结果=从歌曲在XML和LT;诗经&GT;&LT;宋&GT;
选择新与{曲&LT;名称&gt; .value的,
歌曲与LT;年&GT; .value的,
歌曲与LT;类型&GT; .value的,
歌曲与LT;比特率&GT; .value的,
歌曲与LT;长度&GT; .value的,
歌曲与LT;大小&GT;。价值}
这样你就可以访问名称
,年
,类型
等由像一个有意义的名称:
为每首歌曲在结果
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))
下一个
而不是毫无意义的项目1
,项目2
,项目3
等
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
解决方案
It's as easy as
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)
But instead of a monster Tuple
, you could also use an anonymous type. It's easy as
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}
so you can access Name
, Year
, Genre
etc. by a meaningful name like:
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
instead of the meaningless Item1
, Item2
, Item3
etc.
相关推荐
最新文章