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")); NewDoc.AddFirst(new XDocumentType( "MyDocType", "MyPubID", "MySysID", "ThisInternalSubset")); } }
Traversing Forward from an XElement Object via the NextNode Property
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() { XElement firstParticipant; XDocument xDocument = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XDocumentType("Books", null, "Books.dtd", null), new XProcessingInstruction("Book", "out-of-print"), new XElement("Books", firstParticipant = new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "E"), new XElement("LastName", "B")))); Console.WriteLine(firstParticipant.NextNode); } }
Loading a Document with the XDocument.Load Method with LoadOptions
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 = XDocument.Load("book.xml",LoadOptions.SetBaseUri | LoadOptions.SetLineInfo); Console.WriteLine(xDocument); XElement firstName = xDocument.Descendants("FirstName").First(); Console.WriteLine("FirstName Line:{0} Position:{1}",((IXmlLineInfo)firstName).LineNumber,((IXmlLineInfo)firstName).LinePosition); Console.WriteLine("FirstName Base URI:{0}", firstName.BaseUri); } }
Saving a Document with the XDocument.Save Method
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 XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XAttribute("experience", "first-time"), new XAttribute("language", "English"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); xDocument.Save("book.xml"); } }
Intentionally Exposing the Halloween Problem
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 XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "E"), new XElement("LastName", "B")))); IEnumerable<XElement> elements = xDocument.Element("Books").Elements("Book"); foreach (XElement element in elements) { Console.WriteLine("Source element: {0} : value= {1}",element.Name, element.Value); } foreach (XElement element in elements) { Console.WriteLine("Removing {0}= {1} ¡", element.Name, element.Value); element.Remove(); } Console.WriteLine(xDocument); } }
Creating an XML Document with XDocument
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 XDeclaration("1.0", "UTF-8", "yes"), new XDocumentType("Books", null, "Books.dtd", null), new XProcessingInstruction("Book", "out-of-print"), new XElement("Books")); Console.WriteLine(xDocument); } }
Using the LINQ to XML API to Create an XML Document and Adding Some Structure
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 XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); Console.WriteLine(xDocument.ToString()); } }