using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument(); XDocumentType documentType = new XDocumentType("Books", null, "Books.dtd", null); xDocument.Add(documentType, new XElement("Books")); Console.WriteLine(xDocument); } }
Creating a Document Type
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument(new XDocumentType("Books",null,"Books.dtd", null),new XElement("Book")); Console.WriteLine(xDocument); } }
Display the second namespace on screen
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")); XElement ThisElement = new XElement("{http://www.kutayzorlu.com/java2s/com/}First",new XAttribute(XNamespace.Xmlns + "NewNS", "http://www.microsoft.com"),"Hello"); Console.WriteLine(ThisElement.ToString()); XName ThisName = ThisElement.Name; Console.WriteLine(ThisName.LocalName + " Namespace: " + ThisName.Namespace + " Namespace Name: " + ThisName.NamespaceName); ThisName = ThisElement.Attribute(XNamespace.Xmlns + "NewNS").Name; Console.WriteLine("Second Local Name: " + ThisName.LocalName + " Second Namespace: " + ThisName.Namespace + " Second Namespace Name: " + ThisName.NamespaceName + " Attribute Value: " + ThisElement.Attribute(XNamespace.Xmlns + "NewNS").Value); } }
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")); } }