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 name = new XElement("Name", "Joe"); Console.WriteLine(name); Console.WriteLine((string)name); } }
Immediate Execution of the XML Tree Construction
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() { string[] names = { "J", "P", "G", "P" }; XElement xNames = new XElement("Beatles", from n in names select new XElement("Name", n)); names[3] = "R"; Console.WriteLine(xNames); } }
Calling the ToString Method on an Element Produces the XML Tree
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 name = new XElement("Name", "Joe"); Console.WriteLine(name.ToString()); } }
The Elements method returns just the child nodes of type XElement
using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; public class MainClass { public static void Main() { var bench = new XElement("bench", new XElement("A", new XElement("B", "H"), new XElement("B", "R") ), new XElement("A", new XElement("B", "S"), new XElement("C", "N") ), new XComment("comment") ); foreach (XElement e in bench.Elements()) Console.WriteLine(e.Name + "=" + e.Value); } }
Updating a Processing Instruction
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() { XProcessingInstruction procInst; XDocument xDocument = new XDocument( new XElement("Books"), procInst = new XProcessingInstruction("Book", "out-of-print")); Console.WriteLine(xDocument); procInst.Target = "BookContactManager"; procInst.Data = "update"; Console.WriteLine(xDocument); } }
Updating the 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() { XDocumentType docType; XDocument xDocument = new XDocument(docType = new XDocumentType("Books", null,"Books.dtd", null), new XElement("Books")); Console.WriteLine("Before updating document type:"); Console.WriteLine(xDocument); docType.Name = "MyBooks"; docType.SystemId = "http://www.yoursite.com/DTDs/MyBooks.DTD"; docType.PublicId = "-//DTDs//TEXT Book Participants//EN"; Console.WriteLine(xDocument); } }
Updating a Node's Value
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 XElement("Books", firstParticipant = new XElement("Book", new XComment("This is a new author."), new XAttribute("type", "Author"), new XElement("FirstName", "J"), new XElement("LastName", "R")))); Console.WriteLine(xDocument); firstParticipant.Element("FirstName").Value = "J"; firstParticipant.Nodes().OfType<XComment>().Single().Value ="LINQ"; ((XElement)firstParticipant.Element("FirstName").NextNode).Nodes().OfType<XText>().Single().Value = "R"; Console.WriteLine(xDocument); } }