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()); } }
public static AllButtonInfo Deserialize(string xml, string strXmlPath) { AllButtonInfo newItem; string strXmlPathOnly = Path.GetDirectoryName(strXmlPath) + "\\"; DataContractSerializer serializer = new DataContractSerializer(typeof(AllButtonInfo)); StringReader textReader = new StringReader(xml); using (XmlReader xr = XmlReader.Create(textReader)) { newItem = (AllButtonInfo)serializer.ReadObject(xr); } for (int i = 0; i < newItem.buttonInfo.Count(); i++) { ButtonInfo info = newItem.buttonInfo[i]; info.uriSoundFile = new Uri(strXmlPathOnly + Path.GetFileName(info.uriSoundFile.ToString()), UriKind.Absolute); info.uriSoundFile_ShiftKey = new Uri(strXmlPathOnly + Path.GetFileName(info.uriSoundFile_ShiftKey.ToString()), UriKind.Absolute); newItem.buttonInfo[i] = info; } return(newItem); }
private void button1_Click(object sender, EventArgs e) { int idxButton = -1; // -1 means it's unassigned string strNewLabel = string.Empty; string strNewLabel_ShiftKey = string.Empty; Uri uriNewSoundFile = null; Uri uriNewSoundFile_ShiftKey = null; // find the index to button info based on caption if (buttonInfo.Count() > 0) { for (int i = 0; i < buttonInfo.Count(); i++) { if (buttonInfo[i].strKey == (((Button)sender).Text)) { idxButton = i; break; } } } OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "MP3 Files|*.mp3"; openFileDialog1.Title = "Select Non-Shift-Key Sound File for key [" + ((Button)sender).Text + "]"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { uriNewSoundFile = new System.Uri(openFileDialog1.FileName); strNewLabel = Microsoft.VisualBasic.Interaction.InputBox("Label for Non-Shift-Key Sound File?", "Enter Label Text for key [" + ((Button)sender).Text + "]", ""); if (string.IsNullOrEmpty(strNewLabel)) { return; } } else { return; } openFileDialog1.Filter = "MP3 Files|*.mp3"; openFileDialog1.Title = "Select Shift-Key Sound File for key [" + ((Button)sender).Text + "]"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { uriNewSoundFile_ShiftKey = new System.Uri(openFileDialog1.FileName); strNewLabel_ShiftKey = Microsoft.VisualBasic.Interaction.InputBox("Label for Shift-Key Sound File?", "Enter Label Text for key [" + ((Button)sender).Text + "]", ""); if (string.IsNullOrEmpty(strNewLabel_ShiftKey)) { return; } } else { return; } if (idxButton == -1) // unassigned key, need to add new entry to List { buttonInfo.Add(new ButtonInfo { strKey = ((Button)sender).Text.ToLower(), strLabel = strNewLabel, strLabel_ShiftKey = strNewLabel_ShiftKey, uriSoundFile = uriNewSoundFile, uriSoundFile_ShiftKey = uriNewSoundFile_ShiftKey, }); } else { buttonInfo[idxButton] = new ButtonInfo { strKey = ((Button)sender).Text.ToLower(), strLabel = strNewLabel, strLabel_ShiftKey = strNewLabel_ShiftKey, uriSoundFile = uriNewSoundFile, uriSoundFile_ShiftKey = uriNewSoundFile_ShiftKey, }; } ShowLabels(((Button)sender).Text.ToLower(), strNewLabel, strNewLabel_ShiftKey); }