public void DoSerialize() { //var result = new StringBuilder(); ////Type typeObj = obj.GetType() as Type; //DataContractSerializer serializer = new DataContractSerializer(typeof(AllButtonInfo)); //using (var stringWriter = new StringWriter(result)) //using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true })) //{ // serializer.WriteObject(xmlWriter, typeObj); //} AllButtonInfo allInfo = new AllButtonInfo(); allInfo.buttonInfo = buttonInfo; string serialisedToString = allInfo.Serialize(); //MessageBox.Show(serialisedToString); SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "XML Files|*.xml"; saveFileDialog1.Title = "Save Settings File"; saveFileDialog1.ShowDialog(); // If the file name is not an empty string open it for saving. if (saveFileDialog1.FileName != "") { // Saves the Image via a FileStream created by the OpenFile method. System.IO.FileStream fs = (System.IO.FileStream)saveFileDialog1.OpenFile(); byte[] xmlText = new UTF8Encoding(true).GetBytes(serialisedToString); fs.Write(xmlText, 0, xmlText.Length); //// writing data in bytes already //byte[] data = new byte[] { 0x0 }; //fs.Write(data, 0, data.Length); fs.Close(); } }
private void button37_Click(object sender, EventArgs e) // load settings { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "XML Files|*.xml"; openFileDialog1.Title = "Select Settings File"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { strXmlPath = openFileDialog1.FileName; // needed for deserialization to generate absolute paths using (FileStream stream = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read)) { int size = (int)stream.Length; // Returns the length of the file byte[] data = new byte[size]; // Initializes and array in which to store the file stream.Read(data, 0, size); // Begins to read from the constructed stream AllButtonInfo allInfo = new AllButtonInfo(); allInfo = AllButtonInfo.Deserialize(System.Text.Encoding.Default.GetString(data), strXmlPath); buttonInfo.Clear(); buttonInfo = allInfo.buttonInfo; // update the labels for (int i = 0; i < buttonInfo.Count(); i++) { ShowLabels(buttonInfo[i].strKey.ToLower(), buttonInfo[i].strLabel, buttonInfo[i].strLabel_ShiftKey); } } //buttonInfo.Add(new ButtonInfo //{ // strKey = ((Button)sender).Text.ToLower(), // strLabel = strNewLabel, // strLabel_ShiftKey = strNewLabel_ShiftKey, // uriSoundFile = uriNewSoundFile, // uriSoundFile_ShiftKey = uriNewSoundFile_ShiftKey, //}); } }
public string Serialize() { StringBuilder xml = new StringBuilder(); DataContractSerializer serializer = new DataContractSerializer(typeof(AllButtonInfo)); AllButtonInfo objWithTransforms = this; for (int i = 0; i < objWithTransforms.buttonInfo.Count(); i++) { ButtonInfo info = objWithTransforms.buttonInfo[i]; info.uriSoundFile = new Uri(Path.GetFileName(info.uriSoundFile.LocalPath), UriKind.Relative); info.uriSoundFile_ShiftKey = new Uri(Path.GetFileName(info.uriSoundFile_ShiftKey.LocalPath), UriKind.Relative); objWithTransforms.buttonInfo[i] = info; } using (XmlWriter xw = XmlWriter.Create(xml)) { serializer.WriteObject(xw, objWithTransforms); xw.Flush(); return(xml.ToString()); } }