CSharp - Saving with XDocument.Save()

Introduction

You can save your XML document using any of several XDocument.Save methods.

Here is a list of prototypes:

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

The following code is an example where we save the XML document to a file in our project's folder.

Demo

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

class Program/*  w  w w.  j av  a 2 s .c o  m*/
{
    static void Main(string[] args){
        XDocument xDocument = new XDocument(
          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"))));
        
        xDocument.Save("bookparticipants.xml");
    }
}

To disable the format:

xDocument.Save("bookparticipants.xml", SaveOptions.DisableFormatting);

Related Topics