Part 4 Modifying xml document using linq to xml

preview_player
Показать описание
Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

LINQ to SQL Tutorial - All Text Articles & Slides

LINQ to XML Tutorial Playlist

Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses

In Part 3 of LINQ to XML tutorial we discussed querying xml document using linq to xml. In this video we will discuss
1. Adding new xml elements to the xml document
2. Updating xml elements in the xml document
3. Updating xml comments in the xml document
4. Deleting existing xml elements from the xml document

Inserting or adding new xml elements to the xml document : The following code adds the student element at the end of the xml document.

xmlDocument.Element("Students").Add(
new XElement("Student", new XAttribute("Id", 105),
new XElement("Name", "Todd"),
new XElement("Gender", "Male"),
new XElement("TotalMarks", 980)
));

To add the xml element as the first element use AddFirst() method.

To add the xml element in a specific location in the XML Document, use AddBeforeSelf() or AddAfterSelf().
xmlDocument.Element("Students")
.Elements("Student")
.Where(x =] x.Attribute("Id").Value == "103").FirstOrDefault()
.AddBeforeSelf(
new XElement("Student", new XAttribute("Id", 106),
new XElement("Name", "Todd"),
new XElement("Gender", "Male"),
new XElement("TotalMarks", 980)
));

To disable formatting the XML document use SaveOptions.DisableFormatting

Updating an xml element in the xml document :
The following code updates student (with Id = 106) TotalMarks to 999

xmlDocument.Element("Students")
.Elements("Student")
.Where(x =] x.Attribute("Id").Value == "106").FirstOrDefault()
.SetElementValue("TotalMarks", 999);

OR

xmlDocument.Element("Students")
.Elements("Student")
.Where(x =] x.Attribute("Id").Value == "106")
.Select(x =] x.Element("TotalMarks")).FirstOrDefault().SetValue(999);

Updating an xml comment in the xml document :

xmlDocument.Nodes().OfType[XComment]().FirstOrDefault().Value = "Comment Updated";

Deleting xml elements from the xml document

xmlDocument.Root.Elements().Where(x =] x.Attribute("Id").Value == "106").Remove();

The following code removes all "Student" elements that are present under root node "Students"

xmlDocument.Root.Elements().Remove();

Deleting xml comments from the xml document
xmlDocument.Nodes().OfType[XComment]().Remove();
Рекомендации по теме
Комментарии
Автор

This is outstanding. One of the best tutorials I have found on the internet. Saved me hours of searching and reading. Many thanks for clear explanation and code examples.

petedashwood
Автор

All of your YouTube tutorials are of a very high quality. I have already studied dozens of them. I recently reached the stage in a project I am developing where I need to use XML extensively. I have spent 3 days scouring books and the Internet looking for help. I do not know why I did not come to you first - but I got here in the end. You are a superb teacher and have my deepest respect and gratitude.

Noel Macara

noelmacara
Автор

You are my hero, Venkat! The last time I had to do XML processing, all I had was XmlDocument etc. This is so much cleaner and powerful. You saved me so much time and effort. Cheers!

TheSilent
Автор

very nice Article Venkat Kindly provide same quality on WCF REST, WEB API, PAYMENT GATEWAY(PAYPAL, BILL DESK) Integration for us. As your Tutorial is all time best ever tutorial in world.

UmaShankar-bhqv
Автор

Super explanation! Thanks alot! it helped me understand.

lacramioaracorneanu
Автор

Thank you very much ! <3
It's very clear now :)

Devrokk
Автор

This is by far the best tutorial on the internet for LINQ to XML. Insanely well done. Holy fuck!

Sumdottycoy
Автор

Hi!
It is possible to copy one of node?

yaroslavlyulko
Автор

How can i modify nested elements? IT keeps adding new ones because it thinks that they dont exist

Sumdottycoy
Автор

Instead of hardcoding ID number, I use Textbook to get ID from user's input. How should i check for duplicated ID during adding session?

jumbo
Автор

How can a list of classes be serialized using this? for example:

List<Pizza> Pizzas


Pizza
{
string[] toppings;
dough
cheese
etc.
}

Sumdottycoy
Автор

Sir plz name a book for LINQ. (linq to xml, obj and sql)

watan
Автор

The example for modifying the xml doc does not work. xmlDocument.Where does not exist. I just tried this and it does not work.

Dan_B_Rides
Автор

hi how did you add dot LOAD, I am not getting it

SagiShorts
Автор

Getting error in Modifying XML doc: error CS1660: Cannot convert lambda expression to type 'object' because it is not a delegate type.

lokeshsainathgudivada
Автор

How to create
XML file can anybody tell me tomorrow is my exam

sachinpathak
welcome to shbcf.ru