mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-24 04:02:36 +01:00
93 lines
3.2 KiB
C#
93 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using System.Xml;
|
|
|
|
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
|
|
{
|
|
public class TimeXml : SubtitleFormat
|
|
{
|
|
public override string Extension => ".xml";
|
|
|
|
public override string Name => "Xml";
|
|
|
|
public override string ToText(Subtitle subtitle, string title)
|
|
{
|
|
string xmlStructure =
|
|
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + Environment.NewLine +
|
|
"<Subtitle/>";
|
|
|
|
var xml = new XmlDocument();
|
|
xml.LoadXml(xmlStructure);
|
|
|
|
foreach (Paragraph p in subtitle.Paragraphs)
|
|
{
|
|
XmlNode paragraph = xml.CreateElement("Paragraph");
|
|
|
|
XmlNode number = xml.CreateElement("Number");
|
|
number.InnerText = p.Number.ToString(CultureInfo.InvariantCulture);
|
|
paragraph.AppendChild(number);
|
|
|
|
XmlNode start = xml.CreateElement("StartMilliseconds");
|
|
start.InnerText = ((long)(Math.Round(p.StartTime.TotalMilliseconds))).ToString(CultureInfo.InvariantCulture);
|
|
paragraph.AppendChild(start);
|
|
|
|
XmlNode end = xml.CreateElement("EndMilliseconds");
|
|
end.InnerText = ((long)(Math.Round(p.EndTime.TotalMilliseconds))).ToString(CultureInfo.InvariantCulture);
|
|
paragraph.AppendChild(end);
|
|
|
|
XmlNode text = xml.CreateElement("Text");
|
|
text.InnerText = HtmlUtil.RemoveHtmlTags(p.Text);
|
|
paragraph.AppendChild(text);
|
|
|
|
xml.DocumentElement.AppendChild(paragraph);
|
|
}
|
|
|
|
return ToUtf8XmlString(xml);
|
|
}
|
|
|
|
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
|
|
{
|
|
_errorCount = 0;
|
|
|
|
var sb = new StringBuilder();
|
|
lines.ForEach(line => sb.AppendLine(line));
|
|
|
|
string xmlString = sb.ToString();
|
|
if (!xmlString.Contains("<Paragraph>") || !xmlString.Contains("<Text>"))
|
|
return;
|
|
|
|
var xml = new XmlDocument { XmlResolver = null };
|
|
try
|
|
{
|
|
xml.LoadXml(xmlString);
|
|
}
|
|
catch
|
|
{
|
|
_errorCount = 1;
|
|
return;
|
|
}
|
|
|
|
foreach (XmlNode node in xml.DocumentElement.SelectNodes("Paragraph"))
|
|
{
|
|
try
|
|
{
|
|
string start = node.SelectSingleNode("StartMilliseconds").InnerText;
|
|
string end = node.SelectSingleNode("EndMilliseconds").InnerText;
|
|
string text = node.SelectSingleNode("Text").InnerText;
|
|
|
|
subtitle.Paragraphs.Add(new Paragraph(text, Convert.ToDouble(start, CultureInfo.InvariantCulture), Convert.ToDouble(end, CultureInfo.InvariantCulture)));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
System.Diagnostics.Debug.WriteLine(ex.Message);
|
|
_errorCount++;
|
|
}
|
|
}
|
|
subtitle.Renumber();
|
|
}
|
|
|
|
}
|
|
}
|