CS411 Assignment No 1

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Visual Programming (CS411)

Assignment#01
Bc190402697
Total marks = 20

Deadline Date = 15-11-2023

Solution:
using System;
using System.Xml.Linq;
class StudentDataUpdater
{
static void Main(string[] args)
{
// Read student data from XML file
XDocument doc = XDocument.Load("studentData.xml");
XElement students = doc.Element("students");
// Display all student details
Console.WriteLine("Existing Student Details:");
foreach (XElement student in students.Elements("student"))
{
int studentId = Convert.ToInt32(student.Element("id").Value);
string name = student.Element("name").Value;
string vuId = student.Element("vuId").Value;
double cgpa = Convert.ToDouble(student.Element("cgpa").Value);
Console.WriteLine("Student ID: {0}, Name: {1}, VU ID: {2}, CGPA: {3}", studentId,
name, vuId, cgpa);
}
// Prompt user to enter student number for editing
Console.WriteLine("\nEnter the student number you want to edit: ");
int studentNumber = Convert.ToInt32(Console.ReadLine());
// Find the student element with the specified student number
XElement studentToEdit = students.Elements("student").Where(s =>
Convert.ToInt32(s.Element("id").Value) == studentNumber).FirstOrDefault();
if (studentToEdit != null)
{
// Update student details
Console.WriteLine("\nEnter the updated details for student ID {0}:", studentNumber);
Console.WriteLine("Enter updated name: ");
string updatedName = Console.ReadLine();
Console.WriteLine("Enter updated VU ID: ");
string updatedVuId = Console.ReadLine();
Console.WriteLine("Enter updated CGPA: ");
double updatedCgpa = Convert.ToDouble(Console.ReadLine());
// Update student element with new values
studentToEdit.Element("Anas yasir").Value = updatedName;
studentToEdit.Element("BC190402697").Value = updatedVuId;
studentToEdit.Element("2.64").Value = updatedCgpa.ToString();
// Save the updated student data to the XML file
doc.Save("studentData.xml");
Console.WriteLine("\nStudent details updated successfully!");
}
else
{
Console.WriteLine("Student with the specified student number not found.");
}

You might also like