在 VB 中读取 XML

本文关键字:XML 读取 VB | 更新日期: 2023-09-27 18:19:11

我正在尝试在我的应用程序中读取iTunes音乐库XML,但是我不知道如何做到这一点,我正在尝试做的是搜索特定的曲目名称一个规范键,例如"艺术家","专辑"等。 是的,我知道如何使用iTunes COM,但是有些曲目信息无法通过它访问,例如如果曲目是"显式的",则还有一个额外的.key

<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

但它只在列表框中列出了我的所有曲目,任何人都可以帮助我。

在 VB 中读取 XML

哇,这是我见过的最糟糕的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(),以便将其全部加载到内存中,并且在执行更多调用时已对其进行解析。

"我想做的是搜索特定的曲目名称,并检查它的曲目是否有明确的键。">

您可以使用 LINQ to XML 表达式来检查特定轨道是否显式,例如:

Dim xml As XElement = XElement.Load("xmllocation")
Dim trackName = "Say Something"
Dim query = From d In xml...<dict>
            Where d.Elements("key").Any(Function(name) name.Value = "Name" _
                                            AndAlso (CType(name.NextNode, XElement).Value = trackName)) _
                  And d.Elements("key").Any(Function(exp) exp.Value = "Explicit" _
                                             AndAlso (CType(exp.NextNode, XElement).Name = "true"))
Dim isExplicit As Boolean = query.Any()

或者使用等效于上述 LINQ 的 XPath 表达式:

Dim xpath As String = String.Format("//dict[key[.='Name' and following-sibling::string='{0}'] and key[.='Explicit' and following-sibling::true]]", trackName)
Dim isExplicit As Boolean = xml.XPathSelectElements(xpath).Any()

更新:

如果您需要检查曲目之外的艺术家姓名,您可以在 WHERE 子句中添加另一个过滤器,如下所示:

Where ....
      And d.Elements("key").Any(Function(artist) artist.Value = "Artist" _
                                 AndAlso (CType(exp.NextNode, XElement).Value = "artist name"))
      And ...

几种方法可以在 .net 中查询 XML 文档您可以使用直接 DOM 查询

例如:

XmlDOcument doc=new XmlDOcument();
doc.LoadXml("your raw xml");
var plist=doc.DOcumentElement.Children[0]; //my plist
var rootDict=plist.Children[0]; //my root dict
//..
//and so on 

在此处阅读有关 .NET XML DOM API 的信息

http://msdn.microsoft.com/en-us/magazine/cc302158.aspx

您也可以使用 Xml 查询语言:Xpath

http://www.codeproject.com/Articles/9494/Manipulate-XML-data-with-XPath-and-XmlDocument-C