2015-10-06 52 views
0

我做這個代碼中使用標籤庫中檢索.mp3文件的信息,並將其顯示與LINQ的XML文檔的XML,這裏是正確的代碼:檢索數.MP3的信息,並將其存儲到一個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"); 
} 
+0

定義不起作用。這是一個非常模糊的術語。你有錯誤嗎?文件格式是否錯誤?等 – Tim

+0

我的意思是「不工作」是因爲「XDocument結構不正確」錯誤導致程序沒有編譯。 PD:我解決了代碼問題,但現在我的問題是,一個應用程序需求需要在單個xml上保存幾個mp3的信息。 –

回答

0

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")); 
相關問題