public static bool Import(Stream input, Transcription storage) { Transcription.Deserialize(input, storage); return(true); }
new public static WPFTranscription Deserialize(Stream stream) { //on some remote data storage this can be more effective than lot of small direct reads by XMLTextReader #region copy stream to internal buffer byte[] bfr = new byte[32 * 1024]; int read = 0; int initsize = 500 * 1024; MemoryStream bufferStream; try { initsize = (int)stream.Length; }finally { bufferStream = new MemoryStream(initsize); } while ((read = stream.Read(bfr, 0, bfr.Length)) > 0) { bufferStream.Write(bfr, 0, read); } bufferStream.Seek(0, SeekOrigin.Begin); #endregion #if DEBUG #region validation XmlSchemaSet schemas = new XmlSchemaSet(); XNamespace xNamespace = XNamespace.Get("http://www.ite.tul.cz/TRSXSchema3.xsd"); using (var s = File.Open(FilePaths.TrsxSchemaPath, FileMode.Open, FileAccess.Read, FileShare.Read)) schemas.Add(null, XmlReader.Create(s)); XDocument doc = XDocument.Load(bufferStream); foreach (XElement xElement in doc.Descendants()) { // First make sure that the xmlns-attribute is changed xElement.SetAttributeValue("xmlns", xNamespace.NamespaceName); // Then also prefix the name of the element with the namespace xElement.Name = xNamespace + xElement.Name.LocalName; } doc.Validate(schemas, (o, e) => { System.Diagnostics.Debug.WriteLine(string.Format("{0}", e.Message)); }); //restore stream position bufferStream.Seek(0, SeekOrigin.Begin); #endregion #endif var t = new WPFTranscription(); t.BeginUpdate(); Transcription.Deserialize(bufferStream, t); t.IsOnline = t.Elements.ContainsKey("Online") && t.Elements["Online"] == "True"; t.EndUpdate(); t.ClearUndo(); t.Saved = true; return(t); }
/// <summary> /// exportuje dokument do vybraneho formatu - pokud je cesta null, zavola savedialog /// </summary> /// <param name="aDokument"></param> /// <param name="vystup"></param> /// <param name="aFormat"></param> /// <returns></returns> public void ExportovatDokument(string vstup, string vystup, bool times, bool nonoises) { Transcription data = null; using (var file = File.OpenRead(vstup)) data = Transcription.Deserialize(file); if (times) { FileStream fs = new FileStream(vystup, FileMode.Create); StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("windows-1250")); FileInfo fi = new FileInfo(vystup); string pNazev = fi.Name.ToUpper().Remove(fi.Name.Length - fi.Extension.Length); string pHlavicka = "<" + pNazev + ";"; for (int i = 0; i < data.Speakers.Count; i++) { if (i > 0) { pHlavicka += ","; } pHlavicka += " " + data.Speakers[i].FirstName; } pHlavicka += ">"; sw.WriteLine(pHlavicka); sw.WriteLine(); for (int i = 0; i < data.Chapters.Count; i++) { for (int j = 0; j < data.Chapters[i].Sections.Count; j++) { for (int k = 0; k < data.Chapters[i].Sections[j].Paragraphs.Count; k++) { TranscriptionParagraph pP = data.Chapters[i].Sections[j].Paragraphs[k]; //zapsani jednotlivych odstavcu string pRadek = "<" + (pP.InternalID - 1).ToString() + "> "; for (int l = 0; l < pP.Phrases.Count; l++) { TranscriptionPhrase pFraze = pP.Phrases[l]; pRadek += "[" + (pFraze.Begin).ToString() + "]" + pFraze.Text; } sw.WriteLine(pRadek); } } } sw.Close(); } else { StringBuilder sb = new StringBuilder(); foreach (var item in data.EnumerateParagraphs()) { if (nonoises) { var alltext = ignoredGroup.Replace(item.Text, ""); alltext = whitespaceGroup.Replace(alltext, " "); if (!string.IsNullOrWhiteSpace(alltext)) { sb.AppendLine(alltext); } } else { sb.AppendLine(item.Text); } } File.WriteAllText(vystup, sb.ToString()); } }