C# da mavjud faylga matnni qanday qo'shish mumkin?
Download 22.09 Kb.
|
Azizbek
- Bu sahifa navigatsiya:
- . C da XML faylini yozish uchun XmlWriter dan foydalaning.
9. C# da mavjud faylga matnni qanday qo'shish mumkin? using System; using System.IO; class Program { static void Main() { string filePath = "test.txt"; // Faylga yo'l (masalan, "test.txt") string textToAdd = "Bu matn faylga qo'shiladi"; // Matn, faylga qo'shiladigan AppendTextToFile(filePath, textToAdd); } static void AppendTextToFile(string filePath, string text) { // Agar fayl mavjud bo'lsa, uni yaratishni orqadan tashlash. // FileMode.Append ni tanlash orqali matn qo'shiladi. using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write)) { using (StreamWriter streamWriter = new StreamWriter(fileStream)) { streamWriter.WriteLine(text); } } } } 9. C# da XML faylini yozish uchun XmlWriter dan foydalaning. using System; using System.Xml; class Program { static void Main(string[] args) { // XML faylini yaratish XmlWriter xmlWriter = XmlWriter.Create("example.xml"); // XML faylida boshlang'ich elementni qo'shish xmlWriter.WriteStartElement("bookstore"); // Yangi elementlarni qo'shish xmlWriter.WriteStartElement("book"); xmlWriter.WriteElementString("title", "To Kill a Mockingbird"); xmlWriter.WriteElementString("author", "Harper Lee"); xmlWriter.WriteElementString("price", "19.99"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("book"); xmlWriter.WriteElementString("title", "The Great Gatsby"); xmlWriter.WriteElementString("author", "F. Scott Fitzgerald"); xmlWriter.WriteElementString("price", "14.99"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.Close(); Console.WriteLine("XML fayl yaratildi"); } } Download 22.09 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling