Algaritim: Savol-javoblar C# dasturlash tilida oqim nima? Javob


TextFieldParser yordamida CSV faylini o'qish uchun C# dasturini amalga


Download 66.18 Kb.
bet10/18
Sana05.05.2023
Hajmi66.18 Kb.
#1429940
1   ...   6   7   8   9   10   11   12   13   ...   18
Bog'liq
Algaritim amaliy-2

6. TextFieldParser yordamida CSV faylini o'qish uchun C# dasturini amalga
oshiring.
Javob:
using Microsoft.VisualBasic.FileIO; // add this at the start
string filePath = "path/to/your/csvfile.csv";
using (TextFieldParser parser = new TextFieldParser(filePath))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
Console.WriteLine(field);
}
}
}
7. StreamWriter va StringBuilder yordamida CSV faylini yozish uchun C# usulini yarating.
Javob:
using System.IO;
using System.Text;
string filePath = "path/to/your/newcsvfile.csv";
StringBuilder sb = new StringBuilder();
sb.AppendLine("Header1, Header2, Header3"); // add headers
sb.AppendLine("Data1, Data2, Data3"); // add data
sb.AppendLine("Data4, Data5, Data6"); // add data
File.WriteAllText(filePath, sb.ToString());
8. XmlReader yordamida XML faylini o'qish uchun C# tilida dastur yozing.
Javob:
using System.Xml;
string filePath = "path/to/your/xmlfile.xml";
using (XmlReader reader = XmlReader.Create(filePath))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
Console.WriteLine(reader.Name);
}
}
}
9. C# da XML faylini yozish uchun XmlWriter dan foydalaning.
Javob:
using System.Xml;
string filePath = "path/to/your/newxmlfile.xml";
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(filePath, settings))
{
writer.WriteStartElement("Root");
writer.WriteElementString("Data1", "Value1");
writer.WriteEndElement();
}
10. JsonReader yordamida JSON faylini o'qish uchun C# dasturini amalga oshiring.
Javob:
using Newtonsoft.Json;
using System.IO;

string filePath = "path/to/your/jsonfile.json";


using (StreamReader reader = new StreamReader(filePath))
using (JsonReader jsonReader = new JsonTextReader(reader))
{
while (jsonReader.Read())
{
if (jsonReader.Value != null)
{
Console.WriteLine(jsonReader.Value);
}
}
}

Download 66.18 Kb.

Do'stlaringiz bilan baham:
1   ...   6   7   8   9   10   11   12   13   ...   18




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