我需要有关 XML 结构的帮助,因为我完全是 XML 新手,所以我可能会错误地设计结构以供以后在 VB.NET 中阅读。
(这就像我对此做的第四个问题......但不是重复的,请全面了解这一点,我正在认真对待这件事,以我自己的优点来做这件事,我厌倦了写同样的事情这个话题。)
在一个应用程序中,我希望能够管理我的音乐收藏,现在我有很多文本文件(239 个文件),标题如下:
90's Dance
2006 Electro
2007 Hardstyle
2013 Pop (English)
2013 Rock (English)
2013 UK Hardcore
如您所见,由Year Musicstyle.txt
好吧,每个文件都包含这样的数据:
Ab - Stay With Me;320;05:53;13,54
Above & Beyond Feat. Richard Bedford - Sun & Moon (Reaktor Hardcore Remix);320;06:03;13,87
Adrenalin - No Need To Run;320;06:07;14,04
Afrojack & Steve Aoki Feat. Miss Palmer - No Beef (Nobody Bootleg);320;05:45;13,19
Andy Wilson - Forever Loved;192;05:45;7,89
如您所见,由Filename;Bitrate;Duration;SizeInMegabytes
SO的很多人说我最好用SQL或XML创建数据库,这就是我尝试的方法,因为SQL似乎很难开始学习,我对XML或SQL一无所知。
嗯,你可以看到我的 XML 文件需要的结构,xml 文件需要用 and 分隔Year
,然后MusicStyle
我会放歌曲信息。
我已经阅读了在 VB.NET 中编写 xml 文件的教程,但我得到的最终结构如下所示:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Songs Database.-->
<Songs>
<_2013>
<Rock>
<FileName>My Song 1.mp3</FileName>
<Bitrate>320</Bitrate>
<Duration>04:55</Duration>
<Filesize>4,80</Filesize>
<FileName>My Song 2.mp3</FileName>
<Bitrate>192</Bitrate>
<Duration>06:44</Duration>
<Filesize>8,43</Filesize>
<FileName>My Song 3.mp3</FileName>
<Bitrate>128</Bitrate>
<Duration>05:12</Duration>
<Filesize>4,20</Filesize>
<FileName>My Song 4.mp3</FileName>
<Bitrate>256</Bitrate>
<Duration>07:32</Duration>
<Filesize>9,51</Filesize>
</Rock>
</_2013>
</Songs>
但我认为这种结构是不正确的,我不知道,但阅读这样的结构不会更好更容易吗?:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Songs Database.-->
<Songs>
<_2013>
<Rock>
<FileName>My Song 1.mp3</FileName><Bitrate>320</Bitrate><Duration>04:55</Duration><Filesize>4,80</Filesize>
<FileName>My Song 2.mp3</FileName><Bitrate>192</Bitrate><Duration>06:44</Duration><Filesize>8,43</Filesize>
<FileName>My Song 3.mp3</FileName><Bitrate>128</Bitrate><Duration>05:12</Duration><Filesize>4,20</Filesize>
<FileName>My Song 4.mp3</FileName><Bitrate>256</Bitrate><Duration>07:32</Duration><Filesize>9,51</Filesize>
</Rock>
</_2013>
</Songs>
问题是:
我不确定哪个是我的数据库要求的最佳结构设计。
我不确切知道如何设计它,所以我需要帮助来修复代码和设计。
我试过了,但我不能写第二种结构(我说它似乎更容易阅读)
这是我用来编写第一种结构类型的代码:
Imports System
Imports System.Xml
Public Class Form1
' lets create the MyXML.xml document,
Dim xmlfile As String = "c:\MyXML.xml"
' declare our xmlwriter object
Dim Xml As XmlWriter = XmlWriter.Create(xmlfile, _
New XmlWriterSettings() With {.Indent = True})
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
' Write the Xml declaration.
Write_Start(Xml)
' Write a comment.
Write_Comment(Xml, "XML Songs Database.")
' Write the root element.
Write_Element(Xml, "Songs")
' Start our first year element.
Write_Element(Xml, "_2013")
' Start our first music style.
Xml.WriteStartElement("Rock")
' The music style song nodes:
Write_SongInfo_Node(Xml, "My Song 1.mp3", "320", "04:55", "4,80")
Write_SongInfo_Node(Xml, "My Song 2.mp3", "192", "06:44", "8,43")
Write_SongInfo_Node(Xml, "My Song 3.mp3", "128", "05:12", "4,20")
Write_SongInfo_Node(Xml, "My Song 4.mp3", "256", "07:32", "9,51")
' Write the xml end
Write_End(Xml)
' Start the file and exit
Process.Start(xmlfile) : Application.Exit()
End Sub
Private Sub Write_Start(ByVal xml As XmlWriter)
Xml.WriteStartDocument()
End Sub
Private Sub Write_Comment(ByVal xml As XmlWriter, ByVal Comment As String)
xml.WriteComment(Comment)
End Sub
Private Sub Write_Element(ByVal xml As XmlWriter, ByVal element As String)
xml.WriteStartElement(element)
End Sub
Private Sub Write_SongInfo_Node(ByVal xml As XmlWriter,
ByVal Filename As String, _
ByVal Bitrate As String, _
ByVal Duration As String, _
ByVal Filesize As String)
xml.WriteStartElement("FileName")
xml.WriteString(Filename)
xml.WriteEndElement()
xml.WriteStartElement("Bitrate")
xml.WriteString(Bitrate)
xml.WriteEndElement()
xml.WriteStartElement("Duration")
xml.WriteString(Duration)
xml.WriteEndElement()
xml.WriteStartElement("Filesize")
xml.WriteString(Filesize)
xml.WriteEndElement()
End Sub
Private Sub Write_End(ByVal xml As XmlWriter)
xml.WriteEndDocument()
xml.Close()
End Sub
End Class
更新:
我得到这个缩进格式:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Songs Database.-->
<Songs>
<Song>
<FileName>My Song 1.mp3</FileName>
<Year>2007</Year>
<Genre>Dance</Genre>
<Bitrate>320</Bitrate>
<Duration>04:55</Duration>
<Filesize>4,80</Filesize>
</Song>
<Song>
<FileName>My Song 2.mp3</FileName>
<Year>2009</Year>
<Genre>Electro</Genre>
<Bitrate>192</Bitrate>
<Duration>06:44</Duration>
<Filesize>8,43</Filesize>
</Song>
<Song>
<FileName>My Song 3.mp3</FileName>
<Year>2008</Year>
<Genre>UK Hardcore</Genre>
<Bitrate>128</Bitrate>
<Duration>05:12</Duration>
<Filesize>4,20</Filesize>
</Song>
<Song>
<FileName>My Song 4.mp3</FileName>
<Year>2013</Year>
<Genre>Techno</Genre>
<Bitrate>256</Bitrate>
<Duration>07:32</Duration>
<Filesize>9,51</Filesize>
</Song>
</Songs>
但我希望这种格式可以节省“空格”字符的数量,然后节省文件大小:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Songs Database.-->
<Songs>
<Song><FileName>My Song 1.mp3</FileName><Year>2007</Year><Genre>Dance</Genre><Bitrate>320</Bitrate><Duration>04:55</Duration><Filesize>4,80</Filesize></Song>
<Song><FileName>My Song 2.mp3</FileName><Year>2009</Year><Genre>Electro</Genre><Bitrate>192</Bitrate><Duration>06:44</Duration><Filesize>8,43</Filesize></Song>
<Song><FileName>My Song 3.mp3</FileName><Year>2008</Year><Genre>UK Hardcore</Genre><Bitrate>128</Bitrate><Duration>05:12</Duration><Filesize>4,20</Filesize></Song>
<Song><FileName>My Song 4.mp3</FileName><Year>2013</Year><Genre>Techno</Genre><Bitrate>256</Bitrate><Duration>07:32</Duration><Filesize>9,51</Filesize></Song>
</Songs>
这就是我正在尝试的:
Private Sub Write_SongInfo_Node(ByVal xml As XmlWriter,
ByVal Filename As String, _
ByVal Year As String, _
ByVal Genre As String, _
ByVal Bitrate As String, _
ByVal Duration As String, _
ByVal Filesize As String)
xml.WriteStartElement("Song")
' xml.Settings.Indent = False
xml.WriteStartElement("FileName")
xml.WriteString(Filename)
xml.WriteEndElement()
xml.WriteStartElement("Year")
xml.WriteString(Year)
xml.WriteEndElement()
xml.WriteStartElement("Genre")
xml.WriteString(Genre)
xml.WriteEndElement()
xml.WriteStartElement("Bitrate")
xml.WriteString(Bitrate)
xml.WriteEndElement()
xml.WriteStartElement("Duration")
xml.WriteString(Duration)
xml.WriteEndElement()
xml.WriteStartElement("Filesize")
xml.WriteString(Filesize)
xml.WriteEndElement()
' xml.Settings.Indent = True
xml.WriteEndElement()
End Sub