Mega Code Archive

 
Categories / C# / XML LINQ
 

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());    } }