我正在尝试将音乐标签写入XML,但它在无效字符上失败,我尝试过替换,但我似乎无法使syntex正确。
//string pattern = "[\\~#%&*{}/:<>?|\"-]";
//string replacement = "_";
//Regex regEx = new Regex(pattern);
//string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " ");
XDocument baddoc = new XDocument
(new XElement("Corrupt",
badfiles.Select(badfile =>
new XElement("File", badfile))));
baddoc.Save("D:\\badfiles.xml");
// foreach(string musicfile in musicfiles)
//{ String Title = (TagLib.File.Create(musicfile).Tag.Title); }
XDocument doc = new XDocument
(new XElement("Songs",
musicfiles.Select(musicfile=>
new XElement("Song",
(new XElement("Title", (TagLib.File.Create(musicfile).Tag.Title))),
(new XElement("Path", (musicfile))),
(new XElement("Artist", (TagLib.File.Create(musicfile).Tag.Performers)))
))));
doc.Save("D:\\files.xml");
答案 0 :(得分:0)
我最终把这一切都打破了:
XDocument doc = new XDocument();
XElement songsElement = new XElement("Songs");
foreach(var musicfile in musicfiles)
{
XElement songElement = new XElement("Song");
string songTitle;
try { songTitle = (TagLib.File.Create(musicfile).Tag.Title); }
catch { songTitle = "Missing"; }
uint songTNint;
try { songTNint = (TagLib.File.Create(musicfile).Tag.Track); }
catch { songTNint = 00; }
string songTN = songTNint.ToString();
string songPath = musicfile;
string songArtist;
try {songArtist = (TagLib.File.Create(musicfile).Tag.Performers[0]);}
catch {songArtist = "Missing";}
List<string> songGenres = new List<string>();
foreach (string Genre in (TagLib.File.Create(musicfile).Tag.Genres))
{ songGenres.Add(Genre);}
string songGenre;
if (songGenres.Count > 1) { songGenre = (songGenres[0] + "/" + songGenres[1]); }
else { try { songGenre = songGenres[0]; } catch { songGenre = "Missing"; } }
songArtist = Regex.Replace(songArtist, @"[^\u0020-\u007E]", string.Empty);
XElement titleElement = new XElement("Title",songTitle);
XElement tnElement = new XElement("TN", songTN);
XElement pathElement = new XElement("Path", musicfile);
XElement artistElement = new XElement("Artist",songArtist);
XElement genreElement = new XElement("Genre", songGenre);
songElement.Add(titleElement);
songElement.Add(tnElement);
songElement.Add(pathElement);
songElement.Add(artistElement);
songElement.Add(genreElement);
songsElement.Add(songElement);
}