Natijamiz:
XmlWriter sinfi yordamida:
XmlWriter sinfining metodlari:
XmlWriter sinfining metodlaridan va yuqoridagi model va ketma-ketlikdan foydalanib yangi Talabalar.xml nomli hujjat hosil qilamiz.
XmlWriter sinfidan yangi obyekt hosil qilish uchun dastlab uning sozlamalarini hosil qilish kerak bo'ladi:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter obyektini Talabalar.xml fayli nomi va yuqoridagi settings bilan hosil qilamiz:
XmlWriter writer = XmlWriter.Create("Talabalar.xml", settings);
Talaba modelining xususiyatlarini kiritib, hujjatimizni hosil qilamiz:
writer.WriteStartDocument();
writer.WriteStartElement("Talabalar");
writer.WriteStartElement("Talaba");
writer.WriteStartElement("Id");
writer.WriteValue("1");
writer.WriteEndElement();
writer.WriteStartElement("Ism");
writer.WriteValue("Baxtiyor");
writer.WriteEndElement();
writer.WriteStartElement("Familiya");
writer.WriteValue("Murodov");
writer.WriteEndElement();
writer.WriteStartElement("TelNomer");
writer.WriteValue("+998901234567");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
3. XML hujjat hosil qilish uchun C# dasturlash tilida System.Xml.Linq nomlar fazosidagi XElement sinfidan ham foydalansak bo'ladi:
Va nihoyat men yoqtirgan usulga ham yetib keldik 😄!
XElement sinfi yordamida XML hujjat hosil qilish birmuncha oson va tushunarli. Har bir element va uning ichidagi elementlar shajaraga o'xshab joylashadi.
XElement talabalar =
new XElement("Talabalar",
new XElement("Talaba",
new XElement("Id", "1"),
new XElement("Ism", "Baxtiyor"),
new XElement("Familiya", "Murodov"),
new XElement("Tel_nomer", "+998901234567")));
talabalar.Save("Talabalar.xml");
Do'stlaringiz bilan baham: |