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")); // Add the processing instruction. NewDoc.AddFirst(new XProcessingInstruction( "xml-stylesheet", "type='text/xsl' href='MyXSL.XSL'")); } }
XML LINQ
Add an attribute to the root node
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")); // Add an attribute to the root node. NewDoc.Element("Root").Add(new XAttribute("MyAttr", "Attribute Value")); } }
Add a comment to 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 NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Add a comment. NewDoc.AddFirst(new XComment("This is a comment.")); // Add another comment. NewDoc.Element("Root").Add(new XComment("This is another comment.")); // Add a third comment. NewDoc.Add(new XComment("This is the third comment.")); // Add some text. NewDoc.Element("Root").Add(new XText("This is some text.")); } }
Add the CData to 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 NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Add the CData. NewDoc.Element("Root").Add( new XElement("CDataElement", new XCData("This is a CData Entry!"))); } }
Add to First to 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 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); } }