XML Document bilan ishlash

XML Document bilan ishlash

XML (eXtensible Markup Language) — kengaytirilgan markerlash tili degan ma’noni anglatadi va SGML (Standard Generalized Markup Language) standart umumlashtirilgan markerlash tilidan olingan matnga asoslangan markerlash tilidir.

a]:text-teal-600 [&>a:hover]:text-teal-800 [&>code]:bg-teal-600/4 [&>code]:text-inherit [&>code]:shadow-none text-teal-800 fill-teal-700 stroke-teal-700 dark:text-teal-100 dark:[&>a]:text-teal-400 dark:[&>a:hover]:text-teal-500 dark:[&>code]:bg-teal-200/2 dark:[&>code]:text-inherit decoration-teal/6 dark:decoration-teal/6 dark:fill-teal-400 dark:stroke-teal-300 flip-heading-hash»>XML teglari HTML teglari kabi ma’lumotlarni ko’rsatish uchun ishlatilmaydi. Aksincha, XML — ma’lumotlarni saqlash va tartibga solish uchun ishlatiladi. Yaqin kelajakda XML HTML o’rnini bosmaydi, lekin u HTML ning ko’plab muvaffaqiyatli xususiyatlarini qo’llash orqali yangi imkoniyatlarni ochib beradi.

XML hujjatlar bilan ishlash uchun C# dasturlash tilida System.Xml nomlar fazosidagi asosan quyidagi sinflardan foydalaniladi:

Keling endi gapni cho’zmasdan amaliyotga o’tamiz. Quyida biz XML hujjat hosil qilishning bir nechta usullarini ko’rib chiqamiz.

Jarayonda biz Talaba modelidan tuzilgan Talabalar jadvalidan foydalanamiz:

public class Talaba
    {
        public int Id { get; set; }
        public string Ism { get; set; }
        public string Familiya { get; set; }
        public string TelNomer { get; set; }
    }

XmlDocument sinfidan yangi obyekt hosil qilamiz:

XmlDocument document = new XmlDocument();

XmlNode sinfi orqali Xml hujjat parametrlarini hosil qilamiz va uni AppendChild metodi yordamida document ga qo’shamiz:

XmlNode xnode = document.CreateXmlDeclaration("1.0", "UTF-8", null);
document.AppendChild(xnode);
a]:text-periwinkle-700 [&>a:hover]:text-periwinkle-800 [&>code]:bg-periwinkle-700/4 [&>code]:text-inherit [&>code]:shadow-none text-periwinkle-900 fill-periwinkle-700 stroke-periwinkle-700 dark:text-periwinkle-200 dark:[&>a]:text-periwinkle dark:[&>a:hover]:text-periwinkle-600 dark:[&>code]:bg-periwinkle-200/2 dark:[&>code]:text-inherit decoration-periwinkle-700/6 dark:decoration-periwinkle/6 dark:fill-periwinkle-400 dark:stroke-periwinkle-400 flip-heading-hash»>Xml da yangi element hosil qilish XmlNode sinfining CreateElement metodi, elementga qiymat berish uchun esa CreateTextNode metodidan foydalanamiz.

Talabalar nomli bosh elementni yaratamiz va uni documentga qo’shamiz:

XmlNode talabalar = document.CreateElement("Talabar");
document.AppendChild(talabalar);

Endi Talaba modelining xususiyatlarini element shaklida tashkil qilgan Talaba nomli element hosil qilamiz:

XmlNode talaba = document.CreateElement("Talaba");
talabalar.AppendChild(talaba);

XmlNode id = document.CreateElement("Id");
id.AppendChild(document.CreateTextNode("1"));
talaba.AppendChild(id);

XmlNode ism = document.CreateElement("Ism");
ism.AppendChild(document.CreateTextNode("Nodirbek"));
talaba.AppendChild(ism);

XmlNode familiya = document.CreateElement("Familiya");
familiya.AppendChild(document.CreateTextNode("Abdulaxadov"));
talaba.AppendChild(familiya);

XmlNode nomer = document.CreateElement("TelNomer");
nomer.AppendChild(document.CreateTextNode("+998901234567"));
talaba.AppendChild(nomer);

Yaratgan Xml hujjatimizni saqlaymiz:

document.Save("Talabalar.xml");

Natijamiz:

a]:text-periwinkle-700 [&>a:hover]:text-periwinkle-800 [&>code]:bg-periwinkle-700/4 [&>code]:text-inherit [&>code]:shadow-none text-periwinkle-900 fill-periwinkle-700 stroke-periwinkle-700 dark:text-periwinkle-200 dark:[&>a]:text-periwinkle dark:[&>a:hover]:text-periwinkle-600 dark:[&>code]:bg-periwinkle-200/2 dark:[&>code]:text-inherit decoration-periwinkle-700/6 dark:decoration-periwinkle/6 dark:fill-periwinkle-400 dark:stroke-periwinkle-400 flip-heading-hash»>XmlWriter sinfining metodlari:

    Create — Yangi hujjat obyektini yaratish

  • WriteStartDocument — Yangi hujjat yozishni boshlash

  • WriteStartElement — Yangi element ochish

  • WriteValue — Element qiymatini kiritish

  • WriteEndElement — elementni yopish

  • WriteEndDocument — hujjatni yopish

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 😄!

a]:text-teal-600 [&>a:hover]:text-teal-800 [&>code]:bg-teal-600/4 [&>code]:text-inherit [&>code]:shadow-none text-teal-800 fill-teal-700 stroke-teal-700 dark:text-teal-100 dark:[&>a]:text-teal-400 dark:[&>a:hover]:text-teal-500 dark:[&>code]:bg-teal-200/2 dark:[&>code]:text-inherit decoration-teal/6 dark:decoration-teal/6 dark:fill-teal-400 dark:stroke-teal-300 flip-heading-hash»>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");

4. XML hujjatni JSON fayllar kabi Serialize qilish orqali ham yaratishimiz mumkin. Buning uchun bizga System.Xml.Serialization nomlar fazosidagi sinflar kerak bo’ladi.

Dastlab Talaba modelimizga [Serializable] attributini qo’shishimiz kerak bo’ladi:

[Serializable]
public class Talaba
    {
        public int Id { get; set; }
        public string Ism { get; set; }
        public string Familiya { get; set; }
        public string TelNomer { get; set; }
    }

Yangi Talaba obyektini xususiyatlari bilan yaratamiz va Talabalar listiga qo’shamiz:

Talaba talaba = new Talaba
        {
            Id = 1,
            Ism = "Baxtiyor",
            Familiya = "Murodov",
            TelNomer = "+998901234567"
        };
        
ListTalaba> talabalar = new ListTalaba>();
talabalar.Add(talaba);

Obyektimizni serialize qilamiz va TextWriter yordamida Talabalar.xml nomi bilan saqlaymiz:

XmlSerializer serializer = new XmlSerializer(typeof(ListTalaba>));
TextWriter twriter = new StreamWriter("Talabalar.xml");

serializer.Serialize(twriter, talabalar);
twriter.Close();

Natijamiz:

Qachondir davomi bor yana 😉

Dasturlash
XML Document bilan ishlash