检索几个.mp3's信息并将它们存储到xml文档中

本文关键字:存储 文档 xml 信息 几个 mp3 检索 | 更新日期: 2023-09-27 18:16:22

我使用TagLib从.mp3文件检索信息,并使用LINQ to XML将其显示到XML文档中,下面是正确的代码:

static class Program
{   
    [STAThread]
    static void Main()
    {
        TagLib.File file = TagLib.File.Create(@"path of one of my .mp3 files");
        XDocument xdoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Music", new XAttribute("id", 001),
                new XElement("Album", file.Tag.Album),
                new XElement("AlbumArtist", file.Tag.AlbumArtists),
                new XElement("Title", file.Tag.Title),
                new XElement("Track", file.Tag.Track),
                new XElement("Genre", file.Tag.Genres),
                new XElement("Path", Path.GetFullPath("El Gozo (version acústica).mp3")))
            );
        xdoc.Save(@"C:'Users'ivan_000'Music'Data.xml");   
    }
}

但是现在我已经尝试了很多方法来开发相同的程序,将几个。mp3文件存储在同一个XML文档中,但它不起作用。

static void Main()
{
    TagLib.File file = TagLib.File.Create(@"C:'Users'ivan_000'Music'El Gozo  (version acústica).mp3");
    TagLib.File file2 = TagLib.File.Create(@"C:'Users'ivan_000'Music'En el Trono está.mp3");
    TagLib.File file3 = TagLib.File.Create(@"C:'Users'ivan_000'Music'Por Siempre.mp3");
    XDocument xdoc = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XElement("Music", new XAttribute("id", 001),
        new XElement("Album", file.Tag.Album),
        new XElement("AlbumArtist", file.Tag.FirstAlbumArtist),
        new XElement("Title", file.Tag.Title),
        new XElement("Track", file.Tag.Track),
        new XElement("Genre", file.Tag.FirstGenre),
        new XElement("Path", Path.GetFullPath("El Gozo (version acústica).mp3"))),
        new XElement("Music", new XAttribute("id", 002),
        new XElement("Album", file2.Tag.Album),
        new XElement("AlbumArtist", file2.Tag.FirstAlbumArtist),
        new XElement("Title", file2.Tag.Title),
        new XElement("Track", file2.Tag.Track),
        new XElement("Genre", file2.Tag.FirstGenre),
        new XElement("Path", Path.GetFullPath("Por Siempre.mp3"))),
        new XElement("Music", new XAttribute("id", 003),
        new XElement("Album", file3.Tag.Album),
        new XElement("AlbumArtist", file3.Tag.FirstAlbumArtist),
        new XElement("Title", file3.Tag.Title),
        new XElement("Track", file3.Tag.Track),
        new XElement("Genre", file3.Tag.FirstGenre),
        new XElement("Path", Path.GetFullPath("Por Siempre.mp3")))
    );
    xdoc.Save(@"C:'Users'ivan_000'Music'Data.xml");   
} 

检索几个.mp3's信息并将它们存储到xml文档中

一个XML文档只能有一个根元素。你要创建三个。把你的元素放在一个根元素中

var musicDir = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
var paths = Directory.EnumerateFiles(musicDir, @"*.mp3");
var doc = new XDocument(
    new XElement("root", // place the Music elements under root
        from x in paths.Select((p, i) => new { Path = p, Index = i })
        let file = TagLib.File.Create(x.Path)
        select new XElement("Music",
            new XAttribute("id", x.Index + 1),
            new XElement("Album", file.Tag.Album),
            new XElement("AlbumArtist", file.Tag.FirstAlbumArtist),
            new XElement("Title", file.Tag.Title),
            new XElement("Track", file.Tag.Track),
            new XElement("Genre", file.Tag.FirstGenre),
            new XElement("Path", x.Path)
        )
    )
);
doc.Save(Path.Combine(musicDir, "Data.Xml"));