Create an Xml document in a Xml Linq way

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;
public class MainClass{

   public static void Main(string[] args){   

      XDocument ThisDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Contacts",
               new XElement("Contact",
                  new XElement("Name", "J"),
                  new XElement("Address1", "123 First St."),
                  new XElement("Address2", "Suite 3"),
                  new XElement("City", "City"),
                  new XElement("State", "WI"),
                  new XElement("ZIP", "99999-9999")),

                // A second contact.
               new XElement("Contact",
                  new XElement("Name", "G"),
                  new XElement("Address1", "99 North Rd."),
                  new XElement("Address2", "First Floor"),
                  new XElement("City", "Somewhere"),
                  new XElement("State", "CA"),
                  new XElement("ZIP", "88888-8888"))));

      // Display the result.
      Console.WriteLine(ThisDoc.Declaration.ToString() + ThisDoc.Document.ToString());
   }
}

   
     


Save the document where you need it

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument ThisDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Contacts",
               new XElement("Contact",
                  new XElement("Name", "J"),
                  new XElement("Address1", "123 First St."),
                  new XElement("Address2", "Suite 3"),
                  new XElement("City", "City"),
                  new XElement("State", "A"),
                  new XElement("ZIP", "99999-9999")),

                // A second contact.
               new XElement("Contact",
                  new XElement("Name", "G"),
                  new XElement("Address1", "99 North Rd."),
                  new XElement("Address2", "First Floor"),
                  new XElement("City", "Somewhere"),
                  new XElement("State", "B"),
                  new XElement("ZIP", "88888-8888"))));

       Console.WriteLine(ThisDoc.Declaration.ToString() + ThisDoc.Document.ToString());    
       ThisDoc.Save("Test.XML", SaveOptions.None);

   }
}

   
     


Load xml document with XDocument

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
       XDocument ThisDoc = new XDocument();
       ThisDoc = XDocument.Load(Application.StartupPath + @"Test.XML");

   }
}

   
     


Create XDocument with XDeclaration and XElement

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));

   }
}

   
     


Display XDocument

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));

     // Display it on screen.
      Console.WriteLine(NewDoc.Declaration.ToString() +
            "
" + NewDoc.Document.ToString());


   }
}

   
     


Define the namespace

   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));

      XNamespace MyNS = "http://www.kutayzorlu.com/java2s/com/";

      NewDoc.Element("Root").Name = MyNS.GetName("Root");

      // Add an element that uses the namespace.
      NewDoc.Element(MyNS + "Root").Add(
         new XElement(MyNS + "Child1", "Some Data1 "),
         new XElement(MyNS + "Child2", 
            new XAttribute(
               XNamespace.Xmlns + "NewNS", 
               "http://www.kutayzorlu.com/java2s/com"), 
               "Some Data 2"));
   }
}

   
     


Add an element that uses the namespace

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;


public class MainClass{

   public static void Main(string[] args){   
      XDocument NewDoc = new XDocument(
            new XDeclaration("1.0", "utf-8", "yes"),
            new XElement("Root", "MyDoc"));

      XNamespace MyNS = "http://www.kutayzorlu.com/java2s/com/";

      // Add the namespace to the root node.
      NewDoc.Element("Root").Name = MyNS.GetName("Root");

      // Add an element that uses the namespace.
      NewDoc.Element(MyNS + "Root").Add(
         new XElement(MyNS + "Child1", "Some Data1 "),
         new XElement(MyNS + "Child2", new XAttribute(XNamespace.Xmlns + "NewNS", "http://www.kutayzorlu.com/java2s/com"),"Some Data 2"));

   }
}