XDocument document = XDocument.Load("example.xml"); XElement newElement = new XElement("newElement", "some value"); document.Root.Add(newElement); document.Save("example.xml");
XDocument document = XDocument.Load("example.xml"); IEnumerableThis code loads an existing XML document, uses LINQ to filter the elements, and iterates over the resulting collection of elements. The package library for C# XDoc is System.Xml.Linq, which is included in the .NET Framework.fantasyBooks = from book in document.Descendants("book") where (string)book.Attribute("genre") == "fantasy" select book; foreach (XElement book in fantasyBooks) { // Do something with the book element }