public static string Serialize(Bottle b) { return b.ID.ToString().Replace('#', ' ') + "#" + b.AlcoholType.Replace('#', ' ') + "#" + b.Alcohol.Replace('#', ' ') + "#" + b.Content.Replace('#', ' ') + "#" + b.Age.ToString().Replace('#', ' ') + "#" + b.Shell.Replace('#', ' ') + "#" + b.Name.Replace('#', ' ') + "#" + b.Shape.Replace('#', ' ') + "#" + b.Color.Replace('#', ' ') + "#" + b.Material.Replace('#', ' ') + "#" + b.Manufacturer.Replace('#', ' ') + "#" + b.City.Replace('#', ' ') + "#" + b.Country.Replace('#', ' ') + "#" + b.Continent.Replace('#', ' ') + "#" + b.Note.Replace('#', ' ') + "#" + "\n"; }
public static Bottle Deserialize(string serialized) { string[] split = serialized.Split('#'); for (int i = 0; i < split.Count(); i++) { if (string.IsNullOrEmpty(split[i])) { split[i] = string.Empty; } } Bottle b = new Bottle(); try { int testValue; if (int.TryParse(split[0], out testValue)) { b.ID = int.Parse(split[0]); } else { throw new Exception("Invalid value for Age!"); } b.AlcoholType = split[1]; b.Alcohol = split[2]; b.Content = split[3]; if (int.TryParse(split[4], out testValue)) { b.Age = int.Parse(split[4]); } else { throw new Exception("Invalid value for Age!"); } b.Shell = split[5]; b.Name = split[6]; b.Shape = split[7]; b.Color = split[8]; b.Material = split[9]; b.Manufacturer = split[10]; b.City = split[11]; b.Country = split[12]; b.Continent = split[13]; b.Note = split[14]; } catch (IndexOutOfRangeException ex) { return null; } return b; }
private void btnSave_OnClick(object sender, RoutedEventArgs e) { string errorMessage = string.Empty; if (txtID.Text == string.Empty) { errorMessage += "You didn't enter an ID!\n"; } if (txtName.Text == string.Empty) { errorMessage += "You didn't enter a name!\n"; } if (txtAge.Text == string.Empty) { errorMessage += "You didn't enter a name!\n"; } if (!(errorMessage == string.Empty)) { MessageBox.Show(errorMessage, "Error!"); } Bottle b = new Bottle(); try { int testValue; if (int.TryParse(txtAge.Text, out testValue)) { b.Age = int.Parse(txtAge.Text); } else { throw new Exception("Invalid value for Age!"); } b.Alcohol = txtAlcohol.Text; b.AlcoholType = txtAlcoholType.Text; b.City = txtCity.Text; b.Color = txtColor.Text; b.Content = txtContent.Text; b.Continent = cmbContinent.SelectedValue.ToString(); b.Country = txtCountry.Text; if (int.TryParse(txtID.Text, out testValue)) { b.ID = int.Parse(txtID.Text); } else { throw new Exception("Invalid value for ID!"); } b.Manufacturer = txtManufacturer.Text; b.Material = txtMaterial.Text; b.Name = txtName.Text; b.Note = txtNote.Text; b.Shape = txtShape.Text; b.Shell = txtShell.Text; byte[] requestBytes = File.ReadAllBytes(txtBrowse.Text); WebRequests.SendBottleOrImage(new Uri(Constants.Links.SendBottle), Bottle.Serialize(b), Constants.Web.MethodPost, Constants.Web.ContentText); txtNote.Text = WebRequests.SendBottleOrImage(new Uri(Constants.Links.SendImageBase64 + txtID.Text), Convert.ToBase64String(requestBytes, Base64FormattingOptions.None) , Constants.Web.MethodPost, Constants.Web.ContentBinaryFormData); //txtBrowse.Text = } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }