Amaliy matematika fakulteti


XmlDocument sinfidan yangi obyekt hosil qilamiz: XmlDocument document = new XmlDocument(); XmlNode


Download 0.53 Mb.
bet2/7
Sana28.10.2023
Hajmi0.53 Mb.
#1731569
1   2   3   4   5   6   7
Bog'liq
Hosil web dasturlash mustaqil ish1

XmlDocument sinfidan yangi obyekt hosil qilamiz:
XmlDocument document = new XmlDocument();
XmlNode sinfi orqali Xml hujjat parametrlarini hosil qilamiz va uni AppendChild metodi yordamida documentga qo'shamiz:
XmlNode xnode = document.CreateXmlDeclaration("1.0", "UTF-8", null);
document.AppendChild(xnode);
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");

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;
Flush - hujjatni saqlash;
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();

Download 0.53 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7




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