public void WriteToXML() { string wordsFileName; XmlTextWriter writer = null; try { wordsFileName = LingvaOut.ToString() + "-" + LingvaIn.ToString() + ".xml"; writer = new XmlTextWriter(wordsFileName, Encoding.Unicode); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("Translate"); XmlSerializer listSerializer = new XmlSerializer(typeof(List <int>)); foreach (int i in translateDict.Keys) { writer.WriteStartElement("idOut_" + i.ToString()); listSerializer.Serialize(writer, translateDict[i]); writer.WriteEndElement(); } writer.WriteEndElement(); WriteLine("The {0} file is generated!", wordsFileName); } catch (Exception ex) { WriteLine(ex.Message); } finally { if (writer != null) { writer.Close(); } } }
public void ReadFromXML() { int wordID; string wordsFileName; wordsFileName = LingvaOut.ToString() + "-" + LingvaIn.ToString() + ".xml"; XmlTextReader reader = null; List <int> list = new List <int>(); XmlSerializer listSerializer = new XmlSerializer(typeof(List <int>)); try { reader = new XmlTextReader(wordsFileName); reader.WhitespaceHandling = WhitespaceHandling.None; reader.ReadStartElement(); while (reader.NodeType != XmlNodeType.EndElement) { wordID = ToInt32(reader.Name.Split('_')[1]); reader.ReadStartElement(); list = (List <int>)listSerializer.Deserialize(reader); translateDict.Add(wordID, list); list = null; reader.ReadEndElement(); reader.MoveToContent(); } reader.ReadEndElement(); } catch (Exception ex) { WriteLine(ex.Message); } finally { if (reader != null) { reader.Close(); } } }