Back to Browse

Part 4 Modifying xml document using linq to xml

40.3K views
Aug 27, 2014
12:50

Text version of the video http://csharp-video-tutorials.blogspot.com/2014/08/part-4-modifying-xml-document-using.html 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. https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1 Slides http://csharp-video-tutorials.blogspot.com/2014/08/part-4-modifying-xml-document-using_27.html LINQ to SQL Tutorial - All Text Articles & Slides http://csharp-video-tutorials.blogspot.com/2014/08/linq-to-xml-tutorial.html LINQ to XML Tutorial Playlist https://www.youtube.com/playlist?list=PL6n9fhu94yhX-U0Ruy_4eIG8umikVmBrk Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd 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 an 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 an 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();

Download

0 formats

No download links available.

Part 4 Modifying xml document using linq to xml | NatokHD