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); } }
XML LINQ
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); } }
Creating a Document Type and Adding It to a Document
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); } }