public static void WriteJSONFile(AdvertismentsList AdsList, string FileName) { using (StreamWriter writeStr = new StreamWriter(new FileStream(FileName + ".json", FileMode.Create))) { writeStr.Write('['); var lst = AdsList.ToList(); foreach (Advertisment ad in AdsList) { writeStr.WriteLine('{'); writeStr.WriteLine("\t\"{0}\": \"{1}\",", nameof(ad.NameOfAd), ad.NameOfAd); if (ad.Price != "") { writeStr.WriteLine("\t\"{0}\": \"{1}\",", nameof(ad.Price), ad.Price); } writeStr.WriteLine("\t\"{0}\": \"{1}\",", nameof(ad.ServiceType), ad.ServiceType); writeStr.Write("\t\"{0}\": ", nameof(Person)); writeStr.WriteLine('{'); writeStr.WriteLine("\t\t\"{0}\": \"{1}\",", nameof(ad.Who.Name), ad.Who.Name); writeStr.WriteLine("\t\t\"{0}\": \"{1}\",", nameof(ad.Who.Email), ad.Who.Email); writeStr.WriteLine("\t\t\"{0}\": \"{1}\"", nameof(ad.Who.PhoneNumber), ad.Who.PhoneNumber); writeStr.WriteLine("\t\t}"); writeStr.Write('}'); if (!(lst.IndexOf(ad) == lst.Count - 1)) { writeStr.WriteLine(','); } } writeStr.Write(']'); } }
public static void WriteBinaryFile(AdvertismentsList AdsList, string FileName) { BinaryFormatter binForm = new BinaryFormatter(); using (FileStream fs = new FileStream(FileName, FileMode.Create)) { foreach (Advertisment ad in AdsList) { binForm.Serialize(fs, ad); } } }
public static void WriteTxtFile(AdvertismentsList AdsList, string FileName) { StreamWriter writeFile = new StreamWriter(new FileStream(FileName + ".txt", FileMode.Create)); using (writeFile) { foreach (Advertisment ad in AdsList) { writeFile.WriteLine(ad.ToString()); } } }
static void Main(string[] args) { Person Oleg = new Person("Олег", "+380685864200", "*****@*****.**"); Person Dima = new Person("Дмитро", "0963246776", "*****@*****.**"); Person Maria = new Person("Марiя", "0996785467", "*****@*****.**"); Person Fedya = new Person("Федiр", "3769884567", "dfghjdf.fds"); Advertisment ad1 = new Advertisment("Продаж птахiв", ServiceType.Комерція, Oleg, "231$"); Advertisment ad2 = new Advertisment("Чернiвецький сайт знайомств", ServiceType.Знайомства, Dima); Advertisment ad3 = new Advertisment("Вiкна, дверi", ServiceType.Комерція, Maria, "344$"); AdvertismentsList lst = new AdvertismentsList(ReadWriteMethods.WriteBinaryFile, ad1, ad2, ad3); Advertisment ad4 = new Advertisment("Пологовий будинок", ServiceType.Медицина, Fedya, ""); lst.AddAdveertisment(ad4); lst.OrderListBy("Price"); Console.ReadLine(); }
public static void WriteXmlFile(AdvertismentsList AdsList, string FileName) { using (FileStream fileStr = new FileStream(FileName + ".xml", FileMode.Create)) { XmlWriterSettings settings = new XmlWriterSettings(); settings.NewLineOnAttributes = true; settings.Indent = true; XmlWriter writeXml = XmlWriter.Create(fileStr, settings); writeXml.WriteStartDocument(); writeXml.WriteStartElement(nameof(AdvertismentsList)); foreach (Advertisment ad in AdsList) { writeXml.WriteStartElement(nameof(Advertisment)); writeXml.WriteElementString(nameof(ad.NameOfAd), ad.NameOfAd); writeXml.WriteElementString(nameof(ad.ServiceType), ad.ServiceType.ToString()); if (ad.Price != "") { writeXml.WriteElementString(nameof(ad.Price), ad.Price); } writeXml.WriteStartElement(nameof(Person)); writeXml.WriteElementString(nameof(ad.Who.Name), ad.Who.Name); writeXml.WriteElementString(nameof(ad.Who.Email), ad.Who.Email); writeXml.WriteElementString(nameof(ad.Who.PhoneNumber), ad.Who.PhoneNumber); writeXml.WriteEndElement(); writeXml.WriteEndElement(); } writeXml.WriteEndElement(); writeXml.WriteEndDocument(); writeXml.Close(); } }