gpt4 book ai didi

c# - 在 VB 中读取 XML

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:50 25 4
gpt4 key购买 nike

我想在我的应用程序中读取 iTunes 音乐库 XML,但是我不知道如何执行此操作,我想做的是搜索特定轨道名称和规范键,例如“艺术家"、"Album"等。是的,我知道如何使用 iTunes COM,但有些轨道信息无法通过它访问,例如如果轨道是“Explicit”,则有一个附加键

<key>Explicit</key><true/>

我想做的是搜索特定轨道名称并检查它是否有明确的键。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Major Version</key><integer>1</integer>
<key>Minor Version</key><integer>1</integer>
<key>Date</key><date>2014-08-24T05:21:40Z</date>
<key>Application Version</key><string>11.3.1</string>
<key>Features</key><integer>5</integer>
<key>Show Content Ratings</key><true/>
<key>Music Folder</key><string>file://localhost/C:/Users/Mon%20Tolentino/Music/iTunes/iTunes%20Media/</string>
<key>Library Persistent ID</key><string>15CDC06EC711077D</string>
<key>Tracks</key>
<dict>
<key>766</key>
<dict>
<key>Track ID</key><integer>766</integer>
<key>Name</key><string>Say Something</string>
<key>Artist</key><string>A Great Big World</string>
<key>Album Artist</key><string>A Great Big World</string>
<key>Composer</key><string>Ian Axel, Chad Vaccarino, Mike Campbell</string>
<key>Album</key><string>Is There Anybody Out There?</string>
<key>Grouping</key><string>Indie</string>
<key>Genre</key><string>Pop</string>
<key>Kind</key><string>AAC audio file</string>
<key>Size</key><integer>8639959</integer>
<key>Total Time</key><integer>233289</integer>
<key>Disc Number</key><integer>1</integer>
<key>Disc Count</key><integer>1</integer>
<key>Track Number</key><integer>5</integer>
<key>Track Count</key><integer>13</integer>
<key>Year</key><integer>2013</integer>
<key>BPM</key><integer>94</integer>
<key>Date Modified</key><date>2014-05-25T15:58:35Z</date>
<key>Date Added</key><date>2014-05-18T10:09:13Z</date>
<key>Bit Rate</key><integer>256</integer>
<key>Sample Rate</key><integer>44100</integer>
<key>Play Count</key><integer>33</integer>
<key>Play Date</key><integer>3491589574</integer>
<key>Play Date UTC</key><date>2014-08-22T13:59:34Z</date>
<key>Skip Count</key><integer>7</integer>
<key>Skip Date</key><date>2014-08-15T15:15:43Z</date>
<key>Release Date</key><date>2013-11-04T12:00:00Z</date>
<key>Rating</key><integer>60</integer>
<key>Album Rating</key><integer>60</integer>
<key>Album Rating Computed</key><true/>
<key>Artwork Count</key><integer>1</integer>
<key>Sort Album Artist</key><string>A Great Big World</string>
<key>Sort Artist</key><string>A Great Big World</string>
<key>Persistent ID</key><string>50A143EFFA8BEA97</string>
<key>Track Type</key><string>File</string>
<key>Location</key><string>file://localhost/C:/Users/Mon%20Tolentino/Music/iTunes/iTunes%20Media/Music/A%20Great%20Big%20World/Is%20There%20Anybody%20Out%20There_/05%20Say%20Something.m4a</string>
<key>File Folder Count</key><integer>5</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>

我在网上找到的:

    Dim itunesLib As XElement = XElement.Load("xmllocation")

Dim itunesPlaylists = From key In itunesLib...<key> Where key.Value = "Name"
Select key.ElementsAfterSelf("string").Value

List1.ItemsSource = itunesPlaylists

但它只在列表框中列出了我所有的轨道,谁能帮帮我。

最佳答案

哇,这是我见过的最糟糕的 XML。对于不得不忍受这一点,我深表歉意。

我很想从将它们解析为具体类的(可能是延迟的)集合开始,然后是 .Where那些找到你要找的人。

我最初是用 C# 编写所有这些代码的,因为我实际上并不真正了解 VB,然后使用在线工具对其进行了转换,所以请告诉我这是否存在问题或者是否有任何不是最佳实践。不过,它应该传达工作流程的要点,这才是真正的重点。

那么,我将从编写一个类开始:

Public Class Track
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property Explicit() As Boolean
Get
Return m_Explicit
End Get
Set
m_Explicit = Value
End Set
End Property
Private m_Explicit As Boolean
End Class

然后我会编写一个方法来获取 <dict> 中任何键的值元素

Private Function GetValue(Of T)(dict As XElement, key As String) As T
Dim element = dict.Elements("key").FirstOrDefault(Function(c) String.Equals(DirectCast(c, String), key))

If element Is Nothing OrElse element.NextNode Is Nothing Then
Return Nothing
End If

element = element.NextNode

If T = GetType(Boolean) Then
Return element.Name = "true"
Else
Return DirectCast(element, T)
End If
End Function

然后将 XML 解析为该类:

Dim root = XElement.Load(path)

root = root.Element("dict")

Dim tracks = root.Elements("key").First(Function(c) DirectCast(c, String) = "Tracks").NextNode.Elements("dict")

Dim parsedTracks As IEnumerable(Of Track) = tracks.[Select](Function(c) New Track() With { _
Key .Name = GetValue(Of String)(c, "Name"), _
Key .Explicit = GetValue(Of Boolean)(c, "Explicit") _
})

然后你可以很容易地查询:

Return parsedTracks.First(Function(c) c.Name = desiredTrackName).Explicit

我敢肯定,您会想要添加很多错误处理,但这应该传达了一种好的处理方式的要点。您可能想要也可能不想投入 .ToArray()在那里打电话。这真的取决于你如何使用它。这将,就目前而言,每次调用只循环一次(因此“延迟”),所以如果您不经常进行搜索,那很好。但你可能想要 .ToArray()以便它全部加载到内存中,并且如果您进行更多调用则已经解析。

关于c# - 在 VB 中读取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25469173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com