Advertisement
ivandrofly

SUBTITLE EDIT: update xml display format (compare-form)

Nov 18th, 2017
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string path = @"C:\Users\lenovo\Source\Repos\subtitleedit\src\Languages";
  16.             UpdateLanguage(path);
  17.  
  18.  
  19.             Console.WriteLine("DONE");
  20.             Console.ReadLine();
  21.         }
  22.  
  23.         public static void UpdateLanguage(string path)
  24.         {
  25.             string[] files = Directory.GetFiles(path, "*.xml");
  26.  
  27.             const string oldFormat = "{1}%";
  28.             const string newFormat = "{1:0.00}%";
  29.  
  30.             foreach (string xmlFile in files)
  31.             {
  32.                 XDocument xdoc = null;
  33.                 bool changedHappenned = false;
  34.                 try
  35.                 {
  36.                     xdoc = XDocument.Load(xmlFile);
  37.  
  38.                     // element name: XNumberOfDifferenceAndPercentChanged
  39.                     // element value: "Number of differences: {0} ({1:0.00}% of words changed)"
  40.  
  41.                     // TODO: change all {1:0} to {1:0.00}
  42.  
  43.                     XElement rootElement = xdoc.Root;
  44.  
  45.                     XElement compareSubtitleElement = rootElement.Element("CompareSubtitles");
  46.  
  47.                     XElement xElement1 = compareSubtitleElement.Element("XNumberOfDifferenceAndPercentChanged");
  48.                     XElement xElement2 = compareSubtitleElement.Element("XNumberOfDifferenceAndPercentLettersChanged");
  49.  
  50.                     // note: check for nullability because some language files may not have these elements
  51.                     // Console.WriteLine(xElement1?.Value);
  52.                     // Console.WriteLine(xElement2?.Value);
  53.  
  54.                     // chane format
  55.                     if (xElement1 != null)
  56.                     {
  57.                         xElement1.Value = xElement1.Value.Replace(oldFormat, newFormat);
  58.                         changedHappenned = true;
  59.                     }
  60.  
  61.                     if (xElement1 != null)
  62.                     {
  63.                         xElement2.Value = xElement2.Value.Replace(oldFormat, newFormat);
  64.                         changedHappenned = true;
  65.                     }
  66.                 }
  67.                 catch (Exception ex)
  68.                 {
  69.                     Console.WriteLine(ex.Message);
  70.                 }
  71.                 finally
  72.                 {
  73.                     if (changedHappenned)
  74.                         xdoc.Save(xmlFile);
  75.                     // *dispose xdoc
  76.                     xdoc = null;
  77.                 }
  78.             }
  79.         }
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement