正在删除XDocument中XML中的无效字符

本文关键字:无效 字符 XML 删除 XDocument | 更新日期: 2023-09-27 17:59:38

我正试图将音乐标签写入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");

正在删除XDocument中XML中的无效字符

我最终把它分解成这样:

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);
              }