Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 4 - Modifying xml document using linq to xml

Suggested Videos
Part 1 - LINQ to XML
Part 2 - Creating an XML document using in-memory collection of objects
Part 3 - Querying xml document using linq to xml



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.

XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

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

xmlDocument.Save(@"C:\Demo\Demo\Data.xml");

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
xmlDocument.Save(@"C:\Demo\Demo\Data.xml", SaveOptions.DisableFormatting);

Updating xml element in the xml document : 
The following code updates student (with Id = 106) TotalMarks to 999
XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

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

xmlDocument.Save(@"C:\Demo\Demo\Data.xml");

OR

XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

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

xmlDocument.Save(@"C:\Demo\Demo\Data.xml");

Updating xml comment in the xml document : 
XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

xmlDocument.Nodes().OfType<XComment>().FirstOrDefault().Value = "Comment Updated";

xmlDocument.Save(@"C:\Demo\Demo\Data.xml");

Deleting xml elements from the xml document
XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

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

xmlDocument.Save(@"C:\Demo\Demo\Data.xml");

The following code removes all "Student" elements that are present under root node "Students"
XDocument xmlDocument = XDocument.Load(@"C:\Demo\Demo\Data.xml");

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

xmlDocument.Save(@"C:\Demo\Demo\Data.xml");

Deleting xml comments from the xml document
xmlDocument.Nodes().OfType<XComment>().Remove();

LINQ to XML tutorial

1 comment:

  1. Hi
    i need to update the attribute but if that attribute is present i need to insert that attribute into XML file using C#
    can you help us

    ReplyDelete

It would be great if you can help share these free resources