private void Serialize(XmlNode node, string typ) { if (node != null) { node = node.FirstChild; for (; ;) { if (node == null) { break; } if (node.LocalName.ToUpper() == "DATA") { if (typ == "JO") { var data = new JoggenDaten(); if (data.Serialize(node.FirstChild)) { this.JoDaten.Add(data); } } if (typ == "HT") { var data = new HeimtrainerDaten(); if (data.Serialize(node.FirstChild)) { this.HtDaten.Add(data); } } if (typ == "RV") { var data = new RVDaten(); if (data.Serialize(node.FirstChild)) { this.RvDaten.Add(data); } } } node = node.NextSibling; } } }
public void Load(bool useJsonFileFormat = false) { var ecpwin = System.Text.Encoding.GetEncoding(1252); //RVDaten var pfad = this.Pfad + this.Jahr + "\\RV-" + this.Jahr + ".csv"; RvDaten.Clear(); if (File.Exists(pfad)) { var sr = new StreamReader(File.OpenRead(pfad), ecpwin); sr.BaseStream.Seek(0, SeekOrigin.Begin); for (; ;) { var rv = new RVDaten(); if (!rv.Serialize(sr)) { break; } RvDaten.Add(rv); } sr.Close(); } pfad = this.Pfad + this.Jahr + "\\SD.csv"; if (!File.Exists(pfad)) { pfad = this.Pfad + this.Jahr + "\\SD-" + this.Jahr + ".csv"; } SdDaten.Clear(); if (File.Exists(pfad)) { var sr = new StreamReader(File.OpenRead(pfad), ecpwin); sr.BaseStream.Seek(0, SeekOrigin.Begin); for (; ;) { var sd = new SDDaten(); if (!sd.Serialize(sr)) { break; } SdDaten.Add(sd); } sr.Close(); } //HeimtrainerDaten pfad = this.Pfad + this.Jahr + "\\HT-" + this.Jahr + ".csv"; HtDaten.Clear(); if (File.Exists(pfad)) { var sr = new StreamReader(File.OpenRead(pfad), ecpwin); sr.BaseStream.Seek(0, SeekOrigin.Begin); for (; ;) { var htd = new HeimtrainerDaten(); if (!htd.Serialize(sr)) { break; } HtDaten.Add(htd); } sr.Close(); } //JoggenDaten pfad = this.Pfad + this.Jahr + "\\JO-" + this.Jahr + ".csv"; JoDaten.Clear(); if (File.Exists(pfad)) { var sr = new StreamReader(File.OpenRead(pfad), ecpwin); sr.BaseStream.Seek(0, SeekOrigin.Begin); long version = 1; for (; ;) { var htd = new JoggenDaten(); if (!htd.Serialize(sr, ref version)) { break; } //var tmp = htd.Strecke.ToLower().Trim(); JoDaten.Add(htd); } sr.Close(); } #region Load jo.xml, and save jo.json var jo_xml = this.Pfad + this.Jahr + "\\JO-" + this.Jahr + ".xml"; if (!File.Exists(jo_xml)) { jo_xml = this.Pfad + this.Jahr + "\\JO.xml"; } var jsonJoFile = jo_xml.Replace(".xml", ".json"); if (useJsonFileFormat && File.Exists(jsonJoFile)) { var fileContent = File.ReadAllText(jsonJoFile, Encoding.UTF8); this.JoDaten = JsonConvert.DeserializeObject <List <JoggenDaten> >(fileContent); } else if (File.Exists(jo_xml)) { var settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; var reader = XmlReader.Create(jo_xml, settings); var node = FindRoot(reader); Serialize(node, "JO"); reader.Close(); var jsonString = JsonConvert.SerializeObject(this.JoDaten, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(jsonJoFile, jsonString, Encoding.UTF8); } #endregion #region Load ht.xml, and save ht.json var ht_xml = this.Pfad + this.Jahr + "\\HT-" + this.Jahr + ".xml"; if (!File.Exists(ht_xml)) { ht_xml = this.Pfad + this.Jahr + "\\HT.xml"; } var jsonHtFile = ht_xml.Replace(".xml", ".json"); if (useJsonFileFormat && File.Exists(jsonHtFile)) { var fileContent = File.ReadAllText(jsonHtFile, Encoding.UTF8); this.HtDaten = JsonConvert.DeserializeObject <List <HeimtrainerDaten> >(fileContent); } else if (File.Exists(ht_xml)) { var settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; var reader = XmlReader.Create(ht_xml, settings); var node = FindRoot(reader); Serialize(node, "HT"); reader.Close(); var jsonString = JsonConvert.SerializeObject(this.HtDaten, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(jsonHtFile, jsonString, Encoding.UTF8); } #endregion #region Load rv.xml and save rv.json var rv_xml = this.Pfad + this.Jahr + "\\RV-" + this.Jahr + ".xml"; if (!File.Exists(rv_xml)) { rv_xml = this.Pfad + this.Jahr + "\\RV.xml"; } var jsonRvFile = rv_xml.Replace(".xml", ".json"); if (useJsonFileFormat && File.Exists(jsonRvFile)) { var fileContent = File.ReadAllText(jsonRvFile, Encoding.UTF8); this.RvDaten = JsonConvert.DeserializeObject <List <RVDaten> >(fileContent); } else if (File.Exists(rv_xml)) { var settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true; var reader = XmlReader.Create(rv_xml, settings); var node = FindRoot(reader); Serialize(node, "RV"); reader.Close(); var jsonString = JsonConvert.SerializeObject(this.RvDaten, Newtonsoft.Json.Formatting.Indented); File.WriteAllText(jsonRvFile, jsonString, Encoding.UTF8); } #endregion }