Harry Potter and the Sorcerer's Stone


XmlDocument yordamida XML faylini o'qish uchun C# dasturini yozing


Download 192.49 Kb.
bet12/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   8   9   10   11   12   13   14   15   ...   62
Bog'liq
Algaritim amaliy-2

16. XmlDocument yordamida XML faylini o'qish uchun C# dasturini yozing.
Javob:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("file.xml");
XmlNode rootNode = xmlDoc.DocumentElement;
foreach (XmlNode node in rootNode.ChildNodes)
{
Console.WriteLine(node.InnerText);
}
17. XmlDocument yordamida XML faylini yozish uchun C# usulini qo'llang.
Javob:
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("books");
xmlDoc.AppendChild(root);
XmlElement book1 = xmlDoc.CreateElement("book");
book1.SetAttribute("author", "J.K. Rowling");
book1.SetAttribute("title", "Harry Potter and the Philosopher's Stone");
book1.SetAttribute("year", "1997");
root.AppendChild(book1);
XmlElement book2 = xmlDoc.CreateElement("book");
book2.SetAttribute("author", "J.R.R. Tolkien");
book2.SetAttribute("title", "The Lord of the Rings");
book2.SetAttribute("year", "1954");
root.AppendChild(book2);
xmlDoc.Save("books.xml");
18. C# da JSON faylini o'qish uchun JsonReader dasturidan foydalaning.
Javob:
string jsonFilePath = @"C:\path\to\file.json";
using (StreamReader sr = new StreamReader(jsonFilePath))
using (JsonReader reader = new JsonTextReader(sr))
{
JObject obj = JObject.Load(reader);
Console.WriteLine(obj["name"]);
Console.WriteLine(obj["age"]);
Console.WriteLine(obj["location"]["city"]);
}
19. FileStream yordamida ikkilik faylga ma'lumotlarni o'qish va yozish uchun C# dasturini yozing.
Javob:
using System.IO;

class Program {


static void Main(string[] args) {
// Path to binary file
string path = @"C:\example.bin";

// Write data to binary file


using (FileStream fs = new FileStream(path, FileMode.Create)) {
// Data to be written (1, 2, 3)
byte[] data = {1, 2, 3};
fs.Write(data, 0, data.Length);
}

// Read data from binary file


using (FileStream fs = new FileStream(path, FileMode.Open)) {
byte[] inData = new byte[3];
fs.Read(inData, 0, 3);
// Output read data (1, 2, 3)
System.Console.WriteLine(System.BitConverter.ToString(inData));
}
}
}
Izoh: Bu kod C:\example.bin manzilida joylashgan ikkilik faylga 3 bayt ma'lumotni (1, 2, 3) yozadi va keyin uni qayta o'qiydi va konsolda chop etadi.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   8   9   10   11   12   13   14   15   ...   62




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