C# da mavjud faylga matnni qanday qo'shish mumkin?


Download 22.09 Kb.
bet1/3
Sana17.06.2023
Hajmi22.09 Kb.
#1526406
  1   2   3
Bog'liq
Azizbek


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();

// Asosiy elementni yopish


xmlWriter.WriteEndElement();

// XML faylni yopish


xmlWriter.Close();

Console.WriteLine("XML fayl yaratildi");


}
}

Download 22.09 Kb.

Do'stlaringiz bilan baham:
  1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling