DEPARTMENT OF COMPUTER SCIENCE - NUML MULTAN
Course Title: Visual Programming Lab
Course Code: CSVP-368
Instructor: Engr. Syeda Qanitah Naqvi
LAB Task 9
Create/Add a record in XML file, Update a record in XML file,
Delete a record in XML file and Search a record in XML file using
LINQ to XML.
Create/Add a record, Update a record, Delete a record, and Search
a record using LINQ to SQL.
What is LINQ?
LINQ (Language Integrated Query) is a feature in .NET that allows you to
write queries directly in C# to retrieve and manipulate data. Allows writing SQL-
like queries inside C# code. It works with different data sources such as:
Collections (Lists, Arrays),Databases (SQL Server),XML files,Objects,DataSets
Example
var result = from s in students
where [Link] > 20
select s;
What is XML?
XML (Extensible Markup Language) is a markup language used to store and
transport [Link] is designed to store structured data, uses tags like HTML. It is often
used for:
Configuration files
Data sharing between applications
Storing data (like students, employees, products)
Example
<Student>
<ID>1</ID>
<Name>John</Name>
<Age>20</Age>
</Student>
CRUD Operations Using LINQ to XML
LINQ to XML is a part of .NET used to read, query, and modify XML data using
familiar LINQ syntax.
Main classes:
XDocument – Represents an entire XML document.
XElement – Represents an XML element.
XAttribute – Represents an attribute of an element.
Create a Console Application
Open Visual Studio
Click Create a new project
Choose Console App (.NET Framework) or Console App (.NET Core)
Click Next → Create
Step 2: Add an XML File to the Project
In Solution Explorer (Right side of Visual Studio)
Right-click on your project name
Select Add → New Item
Choose XML File
Name it [Link]
Click Add
Your file now appears in Solution Explorer.
XML Example ([Link]):
<Students>
<Student>
<ID>1</ID>
<Name>John</Name>
<Age>20</Age>
</Student>
</Students>
Create/Add a Record in XML
using System;
using [Link];
class CreateXMLRecord
{
static void Main()
{
XDocument doc = [Link]("[Link]");
XElement newStudent = new XElement("Student",
new XElement("ID", 3),
new XElement("Name", "David"),
new XElement("Age", 22));
[Link](newStudent);
[Link]("[Link]");
[Link]("Record Added Successfully!");
}
}
Open [Link] file and you will see record is enetered:
The data enetered is :
Search a Record in XML
using System;
using [Link];
using [Link];
class SearchXMLRecord
{
static void Main()
{
XDocument doc = [Link]("[Link]");
[Link]("Enter ID to search: ");
int id = [Link]([Link]());
var student = [Link]("Student")
.Where(s => (int)[Link]("ID") == id)
.FirstOrDefault();
if (student != null)
[Link](student);
else
[Link]("Record not found!");
}
}
Update a Record in XML
using System;
using [Link];
using [Link];
class UpdateXML
{
static void Main()
{
XDocument doc = [Link]("[Link]");
[Link]("Enter ID to update: ");
int id = [Link]([Link]());
var student = [Link]("Student")
.Where(s => (int)[Link]("ID") == id)
.FirstOrDefault();
if (student != null)
{
[Link]("Name").Value = "Updated Name";
[Link]("Age").Value = "25";
[Link]("[Link]");
[Link]("Record Updated!");
}
else
{
[Link]("Record not found!");
}
}
}
Delete a Record in XML
using System;
using [Link];
using [Link];
class DeleteXML
{
static void Main()
{
XDocument doc = [Link]("[Link]");
[Link]("Enter ID to delete: ");
int id = [Link]([Link]());
var student = [Link]("Student")
.Where(s => (int)[Link]("ID") == id)
.FirstOrDefault();
if (student != null)
{
[Link]();
[Link]("[Link]");
[Link]("Record Deleted!");
}
else
{
[Link]("Record not found!");
}
}
}
OUTPUT:
Records added, updated, deleted, and retrieved successfully from XML.
CRUD Operations Using LINQ to SQL
LINQ to SQL allows querying SQL Server databases using LINQ.
Steps:
Create a Database Table
Add LINQ to SQL Class (.dbml)
Drag the table into the designer
Write LINQ queries in C#
Database Table (Students)
CREATE TABLE Students(
ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT
);
Add/Create Record Using LINQ to SQL
using System;
using [Link];
class AddRecord
{
static void Main()
{
DataClasses1DataContext db = new DataClasses1DataContext();
Student s = new Student()
{
ID = 3,
Name = "David",
Age = 22
};
[Link](s);
[Link]();
[Link]("Record Added Successfully!");
}
}
Search a Record Using LINQ to SQL
class SearchRecord
{
static void Main()
{
DataClasses1DataContext db = new DataClasses1DataContext();
[Link]("Enter ID: ");
int id = [Link]([Link]());
var student = [Link](s => [Link] == id);
if (student != null)
[Link]($"{[Link]} {[Link]}
{[Link]}");
else
[Link]("Record not found!");
}
}
Update a Record Using LINQ to SQL
class UpdateRecord
{
static void Main()
{
DataClasses1DataContext db = new DataClasses1DataContext();
[Link]("Enter ID to update: ");
int id = [Link]([Link]());
var student = [Link](s => [Link] == id);
if (student != null)
{
[Link] = "Updated Name";
[Link] = 25;
[Link]();
[Link]("Record Updated!");
}
else
[Link]("Record not found!");
}
}
Delete a Record Using LINQ to SQL
class DeleteRecord
{
static void Main()
{
DataClasses1DataContext db = new DataClasses1DataContext();
[Link]("Enter ID to delete: ");
int id = [Link]([Link]());
var student = [Link](s => [Link] == id);
if (student != null)
{
[Link](student);
[Link]();
[Link]("Record Deleted!");
}
else
[Link]("Record not found!");
}
}
OUTPUT
CRUD operations successfully performed on SQL Server database using LINQ to
SQL.
VIVA QUESTIONS
What is LINQ to SQL?
What is DataContext?
Difference between LINQ to SQL and Entity Framework?
Why do we use SubmitChanges()?
What is LINQ to XML?
Difference between XElement and XDocument?
How does LINQ improve XML manipulation?
What is the purpose of Descendants()?