public void ImportXML(string xml) { List<Subtitle> subs = new List<Subtitle>(); XmlDocument doc = new XmlDocument(); doc.Load(xml); XmlNode root = doc.ChildNodes[1]; foreach (XmlNode n in root.ChildNodes) { if (n.NodeType != XmlNodeType.Element) continue; Subtitle sub = new Subtitle(); switch (n.Name) { case "Text": sub.type = SubType.Text; sub.data = Helper.Reformat(n.InnerText, 4, true).Replace("\r\n", "\n"); // Check for errors if (!Helper.Check(sub.data, 0)) { string fill_text = sub.data; bool check = false; while (!check && fill_text.Length > 0) { fill_text = fill_text.Remove(fill_text.Length - 1); check = Helper.Check(fill_text, 0); } MessageBox.Show("Text error\n\nWrong line:\n" + sub.data + "\n\nGood line:\n" + fill_text); //continue; } break; case "Comment": sub.type = SubType.Comment; sub.data = Helper.Reformat(n.InnerText, 4, true).Replace("\r\n", "\n"); break; case "Voice": sub.type = SubType.Voice; sub.data = n.Attributes["File"].Value; break; case "Sync": sub.type = SubType.SyncTime; sub.data = n.Attributes["Time"].Value; break; case "Clear": sub.type = SubType.Clear; break; } subs.Add(sub); } this.subs = subs.ToArray(); }
private Subtitle[] Read(string file) { List<Subtitle> subs = new List<Subtitle>(); string text = File.ReadAllText(file, Encoding.GetEncoding("shift_jis")); int pos = 0; while (pos < text.Length) { Subtitle sub = new Subtitle(); string line = Read_String(text, ref pos); if (line.Length == 0) continue; if (line.Length > 0 && line[0] == '#') { sub.type = SubType.Comment; sub.data = Helper.SJISToLatin(line.Substring(1)); } else if (line.StartsWith("/stream")) { sub.type = SubType.Voice; sub.data = line.Substring(8); } else if (line.StartsWith("/sync")) { sub.type = SubType.SyncTime; sub.data = line.Substring(6); } else if (line.StartsWith("/clear")) { sub.type = SubType.Clear; } else { sub.type = SubType.Text; sub.data = Helper.SJISToLatin(line); } subs.Add(sub); listContent.Items.Add(sub.type); } return subs.ToArray(); }