CSharp - Saving with XElement.Save()

Introduction

The XElement class has several Save methods:

void XElement.Save(string filename);
void XElement.Save(TextWriter textWriter);
void XElement.Save(XmlWriter writer);
void XElement.Save(string filename, SaveOptions options);
void XElement.Save(TextWriter textWriter, SaveOptions options);

The following code is an example to save an XML document to a file.

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

class Program/*from  www.  j av  a 2s.c  o m*/
{
    static void Main(string[] args){
          XElement bookParticipants =
            new XElement("Books",
              new XElement("Book",
                new XAttribute("type", "Author"),
                new XAttribute("experience", "first-time"),
                new XAttribute("language", "English"),
                new XElement("FirstName", "Joe"),
                new XElement("LastName", "Ruby")));
    
          bookParticipants.Save("bookparticipants.xml");
    }
}

Related Topic